Background Tasks stuck other requests when i use a http middleware
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
import time
from fastapi import FastAPI, BackgroundTasks, Request, Response
from loguru import logger
app = FastAPI()
@app.middleware("http")
async def logger_request(request: Request, call_next) -> Response:
logger.debug(f"http middleware [{request.url}]")
response = await call_next(request)
return response
class Worker:
def __init__(self, arg):
self.arg = arg
logger.debug(f"worker[{arg}] init")
def work(self):
for i in range(10):
logger.debug(f"worker[{self.arg}] work[{i}]")
time.sleep(1)
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/work/{worker_id}")
async def work(back_task: BackgroundTasks, worker_id: int):
worker = Worker(worker_id)
back_task.add_task(worker.work)
return {"message": f"work[{worker_id}] created"}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
Description
- router “/work/{worker_id}” would create a task(a sync task, actually) on backgroud, after calling it, other new requests will stuck until the task finish
- it just happen when i use a
http middleware
- i knew it is a
starlette
issue but i could not find a solution yet, anybody tell me how to solve this?
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.74.1
Python Version
Python 3.7.10
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
FastAPI Background task in middleware - Stack Overflow
I found some way to solve your problem, i think you can find better way,but for now you can use like this: import...
Read more >Solve the drawbacks with FastAPI BackgroundTasks - Johachi
Drawbacks of FastAPI's BackgroundTask and how to solve them.
Read more >Background tasks occasionally stuck at "submitted"
I can't think of any other reason for background jobs to get stuck as submitted, because tasks only enter the submitted state when...
Read more >Waiting for your ASP.NET Core app to be ready from an ...
NET Core app to be ready to receive requests from inside an ... Whichever approach you take, you can now execute your background...
Read more >Handling Intensive Tasks with Laravel - Toptal
I chose to use Laravel for this particular task for the following reasons: ... of a Laravel application; they observe the HTTP request...
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
Subscribed, thank you.
More about your issue: https://github.com/encode/starlette/pull/1441