The example in the docs shows a nice way of scheduling a long-running task:
async def notify_server_started_after_five_seconds():
await asyncio.sleep(5)
print('Server successfully started!')
app.add_task(notify_server_started_after_five_seconds())
If I've stashed my notify_server_started_after_five_seconds() function in tasks.py, What's the best way to access the app instance from it?
I've looked at dirty solutions like making app global... but I'm surprised that app and loop don't get injected like they do on the decorated methods.
Thanks in advance!
@dvf I think you can already do this with something like:
async def notify_server_started_after_five_seconds(app):
await asyncio.sleep(5)
print(app.name)
app.add_task(notify_server_started_after_five_seconds(app))
However please check out #1063 and let me know if you think that's more helpful. From there the loop is accessible as the app.loop property, so I don't see the reason for passing both.
@r0fls this is awesome. I think #1063 is great! I left a comment about removing loop from the decorated tasks.
P.S. Awesome repo and thanks for the fast response! 馃挴
Most helpful comment
@r0fls this is awesome. I think #1063 is great! I left a comment about removing
loopfrom the decorated tasks.P.S. Awesome repo and thanks for the fast response! 馃挴