[QUESTION] Streaming Response
See original GitHub issueHi,
I am trying to port some aiohttp
code to FastAPI
.
I have a simple (redis) pub-sub system with a producer and consumers. A client connect to a http endpoint then is subscribeb to a channel finally when a producer publishes a message in this channel it gets sent to the connected client as part of the response’s body.
aiohttp provides StreamResponse you can use it the following way:
async def stream_handler(request):
response = web.StreamResponse()
await response.prepare(request)
(channel,) = await redis.subscribe("foobar")
while True:
message = await channel.get()
await response.write(message)
Below is how I tried to port that to FastAPI
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import aioredis
app = FastAPI()
async def messages_from_redis(channel):
while True:
message = await channel.get()
if message:
yield message
print(message)
@app.get("/")
async def main():
redis = await aioredis.create_redis_pool("redis://localhost:6379/")
(channel,) = await redis.subscribe("foobar")
return StreamingResponse(messages_from_redis(channel))
I am not even sure I am using the StreamingResponse
class the right way. It would be really appreciated if someone could help me.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using readable streams - Web APIs | MDN
The Request.body and Response.body properties are available, which are getters exposing the body contents as a readable stream.
Read more >[Code Demo] Using Response Stream from Fetch API - YouTube
[Code Demo] Using Response Stream from Fetch API | JSer - Front-End Interview questions.
Read more >6 Streams API Interview Questions (With Sample Answers)
In this article, we discuss six Streams API interview questions you may encounter and share some example responses to use as a guide....
Read more >Issue in Streaming response from backend server to Client - IBM
We have enbaled streaming in this case so that Datapower does not process the response and streams the response to Client.On the MPG...
Read more >Question Stream™ 1.0 - Fairing
Question Stream takes individual, isolated surveys and turns them into an always-on data source; a string of persistent questions whose responses plug into ......
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
Thanks for the help @Mause! 🚀
Thanks for coming back to close the issue @mekza 🍰
@Mause omg! Thanks for the hint. My http client was buffering…