RuntimeError: Event loop is closed when AsyncClient exists in multiple event_loops. #2959
-
I encountered a small problem. If I run the same AsyncClient in two event loops, an error will be reported in the second event loop (RuntimeError: Event loop is closed). import asyncio
from httpx import AsyncClient
client = AsyncClient()
asyncio.run(client.get('https://www.baidu.com')).status_code
asyncio.run(client.get('https://www.baidu.com')).status_code Error:
Can anyone tell me if this is normal? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hi! This is because httpx uses connection pooling, and it will try to send the second request with the connection that it already has, but since asyncio cannot use the same stream in different event loops, you have an error. To demonstrate it, you can send the special header in your http request, saying that you won't store the underlying connection in the connection pool, so you can make the second request in the separate event loop without any problem. Doesn't work asyncio.run(client.get('https://www.baidu.com')).status_code
asyncio.run(client.get('https://www.baidu.com')).status_code Works asyncio.run(client.get('https://www.baidu.com', headers=[('Connection', 'close')])).status_code
asyncio.run(client.get('https://www.baidu.com')).status_code |
Beta Was this translation helpful? Give feedback.
-
Thank you. |
Beta Was this translation helpful? Give feedback.
-
I have faced the same issue, I tried to use a lot of solutions, it didn't work. Just put these 2 lines in the top of the script: |
Beta Was this translation helpful? Give feedback.
Hi! This is because httpx uses connection pooling, and it will try to send the second request with the connection that it already has, but since asyncio cannot use the same stream in different event loops, you have an error.
To demonstrate it, you can send the special header in your http request, saying that you won't store the underlying connection in the connection pool, so you can make the second request in the separate event loop without any problem.
Doesn't work
Works