You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the FastAPI documentation, with the integrated search.
I already searched in Google "How to X in FastAPI" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to FastAPI but to Pydantic.
I already checked if it is not related to FastAPI but to Swagger UI.
I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
I commit to help with one of those options 👆
Example Code
fromfastapiimportFastAPI, Requestfromfastapi.responsesimportStreamingResponseimportuvicornimportloggingLOGGER=logging.getLogger(__name__)
logging.basicConfig(
encoding="utf-8",
level=logging.DEBUG,
format="%(asctime)s %(levelname)s: %(module)s - line %(lineno)d - %(message)s",
)
app=FastAPI()
asyncdefasync_generator():
for_inrange(20):
yield"hello"asyncdeffunc(request: Request):
print("enter")
# comment the line below, things will goes all fine!js=awaitrequest.json()
print("out")
asyncforiteminasync_generator():
print(item)
ifawaitrequest.is_disconnected():
LOGGER.error("disconnected!")
returntime.sleep(1)
yielditem@app.post("/")asyncdefread_root(request: Request):
generator=func(request)
returnStreamingResponse(generator, media_type="text/event-stream")
if__name__=="__main__":
uvicorn.run(app, host="0.0.0.0", port=23333)
"""You can use the code below to send request to the server for test"""importrequestsURL="http://127.0.0.1:23333"full_response=""withrequests.post(URL, json={"msg": "all"}, stream=True) asr:
ifr.status_code==200:
forlineinr.iter_content(
decode_unicode=True, chunk_size=1024
):
ifline:
print(line)
full_response+=lineprint("full>>>>")
print(full_response)
else:
print(r.status_code)
print(r.json())
Description
When I try to await a Request object in an async generator function, I found the process will get stuck at the line js = await request.json(). But if I place this line of code outside the async generator function or comment this line, things will all be fine, may I ask why this happen?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
When I try to await a Request object in an async generator function, I found the process will get stuck at the line
js = await request.json()
. But if I place this line of code outside the async generator function or comment this line, things will all be fine, may I ask why this happen?Operating System
Windows
Operating System Details
No response
FastAPI Version
0.110.1
Pydantic Version
2.6.4
Python Version
Python 3.11.8
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions