Closing a httpx client results in a "RuntimeError: Event loop is closed" #3402
Unanswered
BabakAmini
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I initially raised this issue on Stack Overflow, but unfortunately, I haven't received a solution. Therefore, I'm bringing it up here again.
I need to maintain a persistent httpx client in my code to utilize its connection pool throughout the lifespan of my application. Below is a simplified version of my implementation:
This results in a
RuntimeError: Event loop is closed
:The only way to resolve this issue is to uncomment the line containing
nest_asyncio.apply()
. However, since thenest_asyncio
package is heavily based on asyncio internal functions and is no longer maintained, I'm not interested in using it in my code.My question is: Why is the loop closed when I call
run_until_complete()
(from within the_close()
function), which creates a new event loop and immediately calls theAyncClient.aclose()
function? How can I fix this?For your reference, the same code works for
aiohttp.ClientSession
without the need to usenest_asyncio
.Beta Was this translation helpful? Give feedback.
All reactions