question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Streaming Response

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Nov 9, 2022

Thanks for the help @Mause! 🚀

Thanks for coming back to close the issue @mekza 🍰

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I’m checking each one in order.

1reaction
mekzacommented, Sep 29, 2020

@Mause omg! Thanks for the hint. My http client was buffering…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found