Asynchronous sleep statements gets cancelled instantly when used with uvicorn and starlette #1795
-
In my project I have a function which must use asyncio.sleep() to perform an async no op. I have the following code to mimic the issue: Working Code
Output
The above given code works fine and successfully streams the video file off of disk. The problem arises when I put in asyncio.sleep() statements in the stream generator function: This code works but cancels the sleep statements
Output
Also the stream generator function works fine when I simply print out the bytes yielded by stream_generator() function so I'm suspecting it's either uvicorn or starlette which is causing this unexpected behaviour. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
What's the version of uvicorn and starlette? If it's not the latest, can you try this again on the latest? |
Beta Was this translation helpful? Give feedback.
-
you cant use |
Beta Was this translation helpful? Give feedback.
-
I upgraded to Python 3.11 and the issue seems to be fixed. Also it may be that having a generator inside an async function is a bad idea, and that't what was causing all the problems. This issue can be closed now. |
Beta Was this translation helpful? Give feedback.
I upgraded to Python 3.11 and the issue seems to be fixed. Also it may be that having a generator inside an async function is a bad idea, and that't what was causing all the problems.
This issue can be closed now.