Skip to content

RuntimeError: Event loop is closed when AsyncClient exists in multiple event_loops. #2959

Closed Answered by karpetrosyan
lee-cq asked this question in Q&A
Discussion options

You must be logged in to vote

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
a…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@tomchristie
Comment options

Answer selected by lee-cq
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants