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:
Peder Bergebakken Sundt 2018-02-25 00:18:05 +01:00
parent 710d207dbd
commit de4ce78bad
6 changed files with 38 additions and 21 deletions

View File

@ -1,5 +1,7 @@
import asyncio
import asyncio, uvloop
from sanic import Sanic
from sanic_openapi import swagger_blueprint, openapi_blueprint
from signal import signal, SIGINT
from . import mpv
from . import nyasync
@ -8,35 +10,38 @@ from . import api
#global variable:
mpv_control = None#mpv.MPVControl()
async def test():
resp = await mpv_control.loadfile('grzegorz/grzegorz/res/logo.jpg')
#print(resp)
def make_sanic_app(host="0.0.0.0", port=8080):
global mpv_control
def main(host="0.0.0.0", port=8080, tasks:list = None):
app = Sanic(__name__)
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():
global mpv_control
mpv_control = mpv.MPVControl()
app.config["mpv_control"] = mpv_control
try:
await mpv_control.run()
except Exception as e:
print(e)
print("mpv is no longer running. Stopping Sanic...")
app.stop()
asyncio.ensure_future(runMPVControl())
return loop, app
if not tasks: tasks = []
tasks.insert(0, runMPVControl())
def main(tasks:list = []):
loop, app = make_sanic_app()
for task in tasks:
app.add_task(task)#instead of ensure_future
app.run(host=host, port=port)
asyncio.ensure_future(task)
loop.run_forever()
if __name__ == '__main__':
main(tasks=[test()])
main()

View File

@ -1,5 +1,6 @@
from sanic import Blueprint, response
import asyncio
from sanic import Blueprint, response
from functools import wraps
from . import mpv
bp = Blueprint("grzegorz-api")
@ -8,6 +9,7 @@ bp = Blueprint("grzegorz-api")
#route decorators:
def response_json(func):
@wraps(func)
async def newfunc(*args, **kwargs):
try:
mpv_control = args[0].app.config["mpv_control"]
@ -26,6 +28,7 @@ def response_json(func):
})
return newfunc
def response_text(func):
@wraps(func)
async def newfunc(*args, **kwargs):
body = await func(*args, **kwargs)
return response.text(body)

12
main.py
View File

@ -1,3 +1,11 @@
#!/usr/bin/env python3
from grzegorz import main, test
main(tasks=[test()])
import asyncio
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()

View File

@ -1,3 +1,4 @@
mpv==0.1
youtube-dl
sanic==0.7.0
sanic-openapi==0.4.0

View File

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB