Error of in-flight requests using streaming response #2118
Answered
by
guyskk
gtsystem
asked this question in
Potential Issue
|
Hi, I'm trying to debug an issue that started showing up from httpx 0.20. import json
import asyncio
import httpx
async def read_lines(client: httpx.AsyncClient):
async with client.stream("GET", "https://httpbin.org/stream/10") as resp:
async for line in resp.aiter_lines():
yield json.loads(line)
async def main():
client = httpx.AsyncClient()
async for json_line in read_lines(client):
print(json_line)
break # we interrupt reading the stream
await client.aclose()
asyncio.run(main())And it fails with From my investigation it seems that Adding Is it a bug or I'm doing something wrong? Any idea? |
Answered by
guyskk
Mar 21, 2022
Replies: 1 comment 1 reply
|
Hi @gtsystem, it seems an asynchronous generators finalization problem, FYI: https://peps.python.org/pep-0525/#finalization Close the generator manually can resolve the problem. async def main():
client = httpx.AsyncClient()
agen = read_lines(client)
async for json_line in agen:
print(json_line)
break # we interrupt reading the stream
await agen.aclose() # --> close it manually
await client.aclose() |
1 reply
Answer selected by
gtsystem
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @gtsystem, it seems an asynchronous generators finalization problem, FYI: https://peps.python.org/pep-0525/#finalization
Close the generator manually can resolve the problem.