Skip to content

StreamingResponse with async generator get stuck indefinitely #1776

Answered by Kludex
iudeen asked this question in Potential Issue
Discussion options

You must be logged in to vote

If you add an await asyncio.sleep(0), does it solve the problem?

async def async_streamer():
    try:
        while True:
            yield b"--boundary\r\nContent-Type: text/plain\r\nContent-Length: 1\r\n\r\n1\r\n"
            await asyncio.sleep(0)  # <--
    except asyncio.CancelledError:
        print("caught cancelled error")
        raise GeneratorExit

Replies: 3 comments 4 replies

Comment options

iudeen
Jul 22, 2022
Collaborator Author

You must be logged in to vote
0 replies
Comment options

Kludex
Jul 22, 2022
Maintainer Sponsor

You must be logged in to vote
4 replies
@iudeen
Comment options

iudeen Jul 22, 2022
Collaborator Author

@Kludex
Comment options

Kludex Jul 22, 2022
Maintainer Sponsor

@iudeen
Comment options

iudeen Jul 22, 2022
Collaborator Author

@Kludex
Comment options

Kludex Jul 22, 2022
Maintainer Sponsor

Answer selected by iudeen
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #1775 on July 22, 2022 15:08.