Make __init__ produce a sanic app object instead of simply blocking as main()
Moved test() coro to main.py outside of grzegors module
This commit is contained in:
parent
710d207dbd
commit
de4ce78bad
@ -1,5 +1,7 @@
|
|||||||
import asyncio
|
import asyncio, uvloop
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
|
from sanic_openapi import swagger_blueprint, openapi_blueprint
|
||||||
|
from signal import signal, SIGINT
|
||||||
|
|
||||||
from . import mpv
|
from . import mpv
|
||||||
from . import nyasync
|
from . import nyasync
|
||||||
@ -8,35 +10,38 @@ from . import api
|
|||||||
#global variable:
|
#global variable:
|
||||||
mpv_control = None#mpv.MPVControl()
|
mpv_control = None#mpv.MPVControl()
|
||||||
|
|
||||||
async def test():
|
def make_sanic_app(host="0.0.0.0", port=8080):
|
||||||
resp = await mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg')
|
global mpv_control
|
||||||
#print(resp)
|
|
||||||
|
|
||||||
def main(host="0.0.0.0", port=8080, tasks:list = None):
|
|
||||||
app = Sanic(__name__)
|
app = Sanic(__name__)
|
||||||
app.blueprint(api.bp, url_prefix="/api")
|
app.blueprint(api.bp, url_prefix="/api")
|
||||||
|
app.blueprint(openapi_blueprint)
|
||||||
|
app.blueprint(swagger_blueprint)
|
||||||
|
|
||||||
#used to ensure sanic/uvloop creates its asyncio loop before MPVControl tries to use one itself
|
asyncio.set_event_loop(uvloop.new_event_loop())
|
||||||
|
server_coro = app.create_server(host=host, port=port)
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
server_task = asyncio.ensure_future(server_coro)
|
||||||
|
signal(SIGINT, lambda s, f: loop.stop())
|
||||||
|
|
||||||
|
mpv_control = mpv.MPVControl()
|
||||||
|
app.config["mpv_control"] = mpv_control
|
||||||
async def runMPVControl():
|
async def runMPVControl():
|
||||||
global mpv_control
|
|
||||||
|
|
||||||
mpv_control = mpv.MPVControl()
|
|
||||||
app.config["mpv_control"] = mpv_control
|
|
||||||
try:
|
try:
|
||||||
await mpv_control.run()
|
await mpv_control.run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
print("mpv is no longer running. Stopping Sanic...")
|
print("mpv is no longer running. Stopping Sanic...")
|
||||||
app.stop()
|
app.stop()
|
||||||
|
asyncio.ensure_future(runMPVControl())
|
||||||
|
|
||||||
if not tasks: tasks = []
|
return loop, app
|
||||||
tasks.insert(0, runMPVControl())
|
|
||||||
|
|
||||||
|
def main(tasks:list = []):
|
||||||
|
loop, app = make_sanic_app()
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
app.add_task(task)#instead of ensure_future
|
asyncio.ensure_future(task)
|
||||||
|
loop.run_forever()
|
||||||
app.run(host=host, port=port)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main(tasks=[test()])
|
main()
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from sanic import Blueprint, response
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from sanic import Blueprint, response
|
||||||
|
from functools import wraps
|
||||||
from . import mpv
|
from . import mpv
|
||||||
|
|
||||||
bp = Blueprint("grzegorz-api")
|
bp = Blueprint("grzegorz-api")
|
||||||
@ -8,6 +9,7 @@ bp = Blueprint("grzegorz-api")
|
|||||||
|
|
||||||
#route decorators:
|
#route decorators:
|
||||||
def response_json(func):
|
def response_json(func):
|
||||||
|
@wraps(func)
|
||||||
async def newfunc(*args, **kwargs):
|
async def newfunc(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
mpv_control = args[0].app.config["mpv_control"]
|
mpv_control = args[0].app.config["mpv_control"]
|
||||||
@ -26,6 +28,7 @@ def response_json(func):
|
|||||||
})
|
})
|
||||||
return newfunc
|
return newfunc
|
||||||
def response_text(func):
|
def response_text(func):
|
||||||
|
@wraps(func)
|
||||||
async def newfunc(*args, **kwargs):
|
async def newfunc(*args, **kwargs):
|
||||||
body = await func(*args, **kwargs)
|
body = await func(*args, **kwargs)
|
||||||
return response.text(body)
|
return response.text(body)
|
||||||
|
12
main.py
12
main.py
@ -1,3 +1,11 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from grzegorz import main, test
|
import asyncio
|
||||||
main(tasks=[test()])
|
import grzegorz
|
||||||
|
|
||||||
|
async def grzegorz_splash():
|
||||||
|
resp = await grzegorz.mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg')
|
||||||
|
#print(resp)
|
||||||
|
|
||||||
|
loop, app = grzegorz.make_sanic_app()
|
||||||
|
asyncio.ensure_future(grzegorz_splash())
|
||||||
|
loop.run_forever()
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
mpv==0.1
|
mpv==0.1
|
||||||
youtube-dl
|
youtube-dl
|
||||||
sanic==0.7.0
|
sanic==0.7.0
|
||||||
|
sanic-openapi==0.4.0
|
||||||
|
Before Width: | Height: | Size: 6.7 KiB After Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Loading…
Reference in New Issue
Block a user