[QUESTION] Long running background tasks
See original GitHub issueHey everyone, I’m currently trying to implement an API endpoint using FastAPI which starts a long running background task using asyncio.create_task(startlongrunningtask()) and then without waiting for that task to finish, return a response to the client. The long running task starts up fine, but times out after e few seconds with the error message [CRITICAL] WORKER TIMEOUT
and Booting worker with pid: 25
How can I prevent the worker from getting timed out?
Is it possible to create such long running tasks which continue even after sending a response to the client?
Thanks a lot and best regards
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
How to Run a Long-Running Background Task in Python
In this tutorial you will discover how to execute long-running tasks in the background in Python. Let's get started. Table of Contents.
Read more >python - Long-running compute-intensive tasks in APIs
So, I thought to use Celery to offload this work to a background worker on a task queue. This works well; the API...
Read more >Background Tasks - FastAPI
You can define background tasks to be run after returning a response. This is useful for operations that need to happen after a...
Read more >c# - How to implement parallel long running background tasks ...
So I have two questions: Is there a way to make these two run in parallel? Are there any other alternatives to run...
Read more >How to avoid long-running background jobs being executed ...
Question How to avoid long-running background jobs being executed on main thread · 1) Optimize the large job (Burst helps a lot) ·...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@xiaodiannao I’m curious to hear if switching to def fixes it.
This isn’t really the intended use case for BackgroundTasks – I think BackgroundTasks is intended more for shorter, low-compute tasks like triggering an email send or similar.
For tasks involving multi-minute calls to subprocesses, I think long term you would benefit from putting in the effort to set up a celery worker and a task queue, and have the celery worker do the subprocess stuff. (I have a similar setup on one of my own projects, which runs multi-minute compute-intensive jobs.) That will decouple your server from the worker, and may help you avoid/preempt a lot of issues arising from long-running and/or compute-intensive jobs running alongside your server.
If you wanted to do this, the full stack fastapi postgres template has a celery worker; you should be able to adapt it to your use case.
happy to help but you’d need to give more info on what the tasks does, how you’re running the app, what worker stops (uvicorn one or gunicorn)
the below snippet shows a long running background task in action, it just sleeps and doesn’t use
asyncio.create_task
like you describe though