-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
middleware breaks behavior of background task #2215
Comments
I believe this is related to Starlette Issue 919: Background tasks don't work with middleware that subclasses |
thank you @ycd seems like work is still in progress |
@jkh911208 Could you close this issue, please? |
For those who are searching for how to call background tasks under FastAPI middleware, here is quick solution: from starlette.background import BackgroundTask @app.middleware("http") You can add a time.sleep(10) in function A to see the result better. Hope this helps someone! |
Hi @jkh911208 @Kludex , why is this issue |
What is the version of your FastAPI? Oh sorry, yeah, this is still a thing. encode/starlette#1441 |
I'm on v0.75.2 |
Yes, don't use |
In that case I couldn't add general before and after request actions, right? |
No, I'm saying to not use that class. You can have middlewares: https://pgjones.dev/blog/how-to-write-asgi-middleware-2021 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Example
Description
run the above code, make a request to "/"
you will see
which logging is happening before return the object
but if you make only one request at a time this is fine, but if you make multiple request while background task is running
problem #1 the second request start after first background task finishes
problem #2 when background task of second request finishes, it throw error
Environment
Additional context
if i remove middleware code it works as expected
all requests are handled and all background tasks are working fine no exception
The text was updated successfully, but these errors were encountered: