sanic
sanic copied to clipboard
Can background tasks set timed tasks? Just like crontab time expression
Can background tasks set timed tasks? Just like crontab time expression
The feature is mainly just a thin wrapper around loop.create_task
. So, the answer is no. This does sound (however) like an interesting idea for a plugin.
Ok, I see So what should I do to keep executing in a loop? The following is the code of the reference document, but only output once
async def generate_code(app):
await asyncio.sleep(1)
print('Server successfully started!')
app.add_task(generate_code(app))
Or can it only be done through Python's own loop? E.g:
async def generate_code(app):
while True:
await asyncio.sleep(5)
print('Server successfully started!')
app.add_task(generate_code(app))
@jakehu the create_task
method does not share the same idea of a task we have from crontab
- it's a one shot only (as soon as the asyncio
event loop is running). so, you either wrap it with a while True:
or call your method again within a new task - IMO the later might not be a good idea since it can possibly create an infinite number of nested tasks within that first task, thus leaking some memory (that was kind of something like two or three years ago, it might have changed now).
Or can it only be done through Python's own loop? E.g:
async def generate_code(app): while True: await asyncio.sleep(5) print('Server successfully started!') app.add_task(generate_code(app))
This is what I would do :muscle:
If we are to implement it in Sanic, this could be a good starting point: https://github.com/Rapptz/discord.py/blob/master/discord/ext/tasks/init.py
Documentation at https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html
If we are to implement it in Sanic, this could be a good starting point: https://github.com/Rapptz/discord.py/blob/master/discord/ext/tasks/init.py
Documentation at https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html
This is really a good idea
This is really a good idea
It feels more like something we should add to sanic-ext
.
Honestly, it is partially something I have been kicking around in my head for a long time as its own full blown project. But, there is probably a simpler implementation we could achieve first.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.
...
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is incorrect, please respond with an update. Thank you for your contributions.