Miksus / rocketry

Modern scheduling library for Python
https://rocketry.readthedocs.io
MIT License
3.25k stars 105 forks source link

ENH Support of background tasks #121

Closed Hollander-1908 closed 1 year ago

Hollander-1908 commented 1 year ago

Is your feature request related to a problem? Please describe. It seems not possible to run tasks in the background.

Describe the solution you'd like I would like to run the tasks in the background so it can do other stuff in the meantime.

Describe alternatives you've considered I use APScheduler right now but besides it supports background tasks I feel like Rocketry would be way more convenient if it supports this feature.

Miksus commented 1 year ago

Do you mean that to run a (Rocketry) task permanently alongside the rest of the scheduler? If so, Rocketry indeed fully supports it using permanent_task. Possibly not well documented yet.

@app.task(on_startup=True, permanent_task=True, execution="async")
async def do_background():
    while True:
        ...

The permanent_task=True is nothing super special. It just means that the task cannot be timeouted and when the scheduler shuts down, the engine will not wait for it to finish and it will terminate it outright. It supports all of the execution methods.

Or did you mean something else?

Hollander-1908 commented 1 year ago

Actually it should be the other way around like APScheduler is doing with their BackgroundScheduler().

So the scheduler should be in the background. Not other tasks. I tried above method but it doesn't seem to start my web app, it's just hanging.

Miksus commented 1 year ago

Rocketry's scheduler does not make assumptions on which application runs on top of it. It's the responsibility of the main application to inform Rocketry's scheduler to quit scheduling. You can use the shut_cond or the shut_down method. There are various ways to signal for exit and Rocketry should not make assumptions regarding this or force you to use specific one.

Perhaps there could be a collection of conditions for most common kill signals but it should be easy to create your own condition and add that as a shut_cond.

I can later elaborate a bit more (on phone). What technologies are you using for the web app?

If you embed your web app to Rocketry's task, you probably need to do the reverse: ask the web app to exit from the Rocketry task. And it depends on the web library how its shutdown mechanism works.

Miksus commented 1 year ago

Without further information, I'll close this issue.

Feel free to open another one if this was not solved but include the web technology that was used so we can see whether the issue is in Rocketry or in the web technology (like does it support adding callbacks for shutdown sequence).

By design Rocketry is well suitable to be embedded in other applications. I have demonstrated it with FastAPI: https://github.com/Miksus/rocketry-with-fastapi