Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jakehu opened this issue Sep 8, 2021 · 12 comments
Closed

Comments

@jakehu
Copy link

jakehu commented Sep 8, 2021

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

@ahopkins
Copy link
Member

ahopkins commented Sep 9, 2021

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.

@jakehu
Copy link
Author

jakehu commented Sep 9, 2021

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
Copy link
Author

jakehu commented Sep 10, 2021

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

@vltr
Copy link
Member

vltr commented Sep 10, 2021

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

@ahopkins
Copy link
Member

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 💪

@prryplatypus
Copy link
Member

prryplatypus commented Nov 1, 2021

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

@jakehu
Copy link
Author

jakehu commented Nov 5, 2021

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

@ahopkins
Copy link
Member

ahopkins commented Nov 5, 2021

This is really a good idea

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

@ahopkins
Copy link
Member

ahopkins commented Nov 5, 2021

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.

@stale
Copy link

stale bot commented Mar 2, 2022

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 stale bot added the stale label Mar 2, 2022
@ahopkins
Copy link
Member

ahopkins commented Mar 2, 2022

...

@stale stale bot removed the stale label Mar 2, 2022
@stale
Copy link

stale bot commented Jun 12, 2022

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 stale bot added the stale label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants