sanic icon indicating copy to clipboard operation
sanic copied to clipboard

Can background tasks set timed tasks? Just like crontab time expression

Open jakehu opened this issue 2 years ago • 12 comments

Can background tasks set timed tasks? Just like crontab time expression

jakehu avatar Sep 08 '21 03:09 jakehu

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.

ahopkins avatar Sep 09 '21 02:09 ahopkins

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))

jakehu avatar Sep 09 '21 07:09 jakehu

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 avatar Sep 10 '21 06:09 jakehu

@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).

vltr avatar Sep 10 '21 13:09 vltr

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:

ahopkins avatar Sep 10 '21 14:09 ahopkins

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

prryplatypus avatar Nov 01 '21 19:11 prryplatypus

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

jakehu avatar Nov 05 '21 08:11 jakehu

This is really a good idea

It feels more like something we should add to sanic-ext.

ahopkins avatar Nov 05 '21 08:11 ahopkins

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.

ahopkins avatar Nov 05 '21 08:11 ahopkins

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.

stale[bot] avatar Mar 02 '22 08:03 stale[bot]

...

ahopkins avatar Mar 02 '22 09:03 ahopkins

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.

stale[bot] avatar Jun 12 '22 21:06 stale[bot]