Error of in-flight requests using streaming response #2118
-
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? |
Beta Was this translation helpful? Give feedback.
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() |
Beta Was this translation helpful? Give feedback.
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.