-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Sanic-like background tasks #4475
Comments
That's still the case. Not FastAPI's issue, but actually the server itself. Why running a long task using |
For asynchronous tasks, I personally use Celery. There are also FastAPI + Celery specific tutorials available online as well :D |
Something like that may be nice. The use case I have is I have some infrequently updated database information that the application needs but I don't want to request the data all the time as it only changes once a day or so. I would like to periodically update these values. |
Sanic just wraps https://github.com/sanic-org/sanic/blob/ac388d644b1e22156e228470fad8ea34932c080a/sanic/app.py#L1159 from fastapi import (
FastAPI as BaseFastAPI
)
import asyncio
class FastAPI(BaseFastAPI):
def add_task(
self,
coro,
):
asyncio.create_task(coro) |
Sanic does a bit more than just wrap It also keeps track of those tasks in a registry, and cleans them up when the application shuts down. That's probably something that can be manually copied too, but it would be nice for that to be handled by the framework.
Yes, that's the idea 🙂 Like with Sanic, it would be great if the application could keep track of and handle the cleanup of the async tasks instead of having to do it manually -- although I do understand that for some cases that might not be too hard. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
In Sanic, the concept of a background task refers to something different than in FastAPI -- namely, the ability to add a task to the event loop outside of the request/response concept, such as on server start.
From Sanic docs:
You can check out Sanic docs for more details.
In one of my applications, I use this kind of background task to run an async function that checks for updates to certain configs in the Firestore database. You can see sample code for that task above. This task runs on server start, and stays in the background for the whole life of the application.
Wanted Solution
I am exploring migrating this application from Sanic to FastAPI, and would like the ability to run this kind of background task. It's unfortunate that the term "background tasks" in FastAPI/Starlette becomes overloaded. FastAPI could call them 'async tasks' or something else.
I'm also not sure how this plays into FastAPI seemingly not having the concept of an application context (assuming that is still the case?)
Wanted Code
Alternatives
Perhaps using
Depends
as described here?Operating System
Linux, macOS
Operating System Details
No response
FastAPI Version
0.72.0
Python Version
Python 3.9.7
Additional Context
No response
The text was updated successfully, but these errors were encountered: