Comments (19)
I don't think streaming responses should be considered similar to background tasks. They are part of the request/response cycle. I think their behavior in this case being similar to background tasks not being able to use dependency injection in the path operators is likely a bug.
from fastapi.
I commented in the Discussion that
StreamingResponse
's no longer work withDepends
resources. Since this is a pretty large change in behavior, and wasn't called out in the release notes, I wonder if this is intended. Should this be a separate Issue?
No. The only reason I created this issue is because of your comment. 🙏
from fastapi.
I might be misinterpreting but I don't think so? Seems like it was intended to disable using resources in background tasks. But was it intended to also disable using resources in path operators with StreamingResponses
?
from fastapi.
A workaround for now is to use our connection pool directly in each endpoint doing streaming. Annoying, but works.
Doing something like this for a psycopg pool:
@router.get("/stuff", response_model=list[Stuff])
async def get_all_stuff(pool: PoolDep):
connection = await pool.getconn()
async def commit_and_put_in_pool():
await connection.commit()
await pool.putconn(connection)
return StreamingResponse(
stream_stuff(connection), media_type="application/json", background=commit_and_put_in_pool
)
This seems to work for us. But is it correct? Will the background task always run after the streaming is done? Or can we end up in a situation where it is run in the middle of streaming?
from fastapi.
Our streaming endpoints are broken probably due to this. So it seems that there is no
good
workaround as long as a db session is managed withDepends
?
The release notes suggest the following:
If you used to rely on this behavior, now you should create the resources for background tasks inside the background task itself, and use internally only data that doesn't depend on the resources of dependencies with yield.
For example, instead of using the same database session, you would create a new database session inside of the background task, and you would obtain the objects from the database using this new session. And then instead of passing the object from the database as a parameter to the background task function, you would pass the ID of that object and then obtain the object again inside the background task function.
i.e. just don't use Depends
for obtaining your db session for background tasks any more but obtain it within the background task
from fastapi.
Any progress on this? Our workaround is not working as expected. It seems the background task is not guaranteed to run, and thus our connection pool is failing when ever there is any error, essentially taking down the entire program, and we must restart.
This basically renders streaming useless for us.
Is there something else we can do to make it work?
from fastapi.
I commented in the Discussion that StreamingResponse
's no longer work with Depends
resources. Since this is a pretty large change in behavior, and wasn't called out in the release notes, I wonder if this is intended. Should this be a separate Issue?
from fastapi.
Does this comment by @tiangolo answers this issue: #11177 (reply in thread) ?
from fastapi.
It seems this is what breaks our streaming endpoints, the lack of which kills throughput making everything slow.
from fastapi.
Our streaming endpoints are broken probably due to this.
So it seems that there is no good
workaround as long as a db session is managed with Depends
?
from fastapi.
@scriptator we're not talking about background tasks here.
from fastapi.
@scriptator we're not talking about background tasks here.
I was replying to @brian-goo who talked about streaming responses which behave like background tasks in this regard. I had a problem because I used a db session in a streaming response that I obtained with Depends and it was fixed by following the advice for background tasks: initiate your resources within the background task (or generator function in the case of streaming responses).
But you are right if you meant that this does not contribute to the original problem from the issue.
from fastapi.
I am facing the same problem. Does anybody know any workaround for this problem?
@tiangolo
#11444
from fastapi.
For now, just staying on version <0.106
.
from fastapi.
Does <0.106
work on python 3.12? Because 0.109 says "Add support for Python 3.12."
from fastapi.
+1 for this issue. It breaks request context management for stream proxies.
ie I have a Depends
parameter for connecting to an external streaming API. I start a stream from an external API in the route. I need the initial response from the external API to contribute to the StreamResponse
return. Then I need that same connection to stream from my streaming generator.
The work around in my case is to use a global client for the external streaming API so that cleanup is not called on the return of StreamingResponse
. As well as managing the context with try/finally blocks in the route and streaming generator. Then more context managers in the streaming generator for other Depends
that die when the route returns.
from fastapi.
Related Issues (20)
- No streaming interface can not support concurrency for two fastapi servers
- Using pydantic Json Type as Form data type doesn't work HOT 1
- Raw docstring (leading `r`) defeats form feed `\f` truncation HOT 3
- OpenAPI Example with multipart/form-data not showing up HOT 5
- [BUG] Using Nested Pydantic models and `params: MyModel = Depends()` forces OpenAPI docs GET methods to require a request body. HOT 6
- How to fix this bug? HOT 2
- [BUG] Upgrade python-mulipart==0.0.7 from low version fastapi upload file may be 400 HOT 4
- Use `RootModel` as query parameter HOT 3
- Middleware runs twice HOT 8
- Support for Pydantic deprecated fields HOT 1
- axios can't receive error response status code
- Potential footgun when using custom `Response(background=<Task()>)` in conjunction with injected `BackgroundTasks` - the custom response overwrites the tasks HOT 3
- lifespan
- Breaking change with path parameters when updating to pydantic>=2 from pydantic<2 HOT 1
- trying to live video stream using Fastapi
- Package on test.pypi.org is broken
- middleware type
- It throws an exception when I specify return Http status code
- When backend restart the frontend request a protected api cause an unhandle exception: Exception in ASGI application HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi.