Event loop is closed error when running with global client #3163
Replies: 2 comments 4 replies
-
Hm... I'd be curious what's actually occurring here. I'd be surprised if outgoing HTTP connections are being maintained across lambda invocations in most deployment environments. I think the root of what's actually being highlighted here is that for the async case (where we're forced to be more explicit about I/O usage and resource lifetimes) we should probably gracefully transition towards only allowing the context-managed style for the client. (Eg. see also Forcing this usage... async with httpx.AsyncClient() as client:
... Would ensure that async clients have explicitly clear lifetimes, and aren't used across different invocations of an event loop. |
Beta Was this translation helpful? Give feedback.
-
Just ran into this as well with a flask app running in docker on aws lambda. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I am developing a function for AWS lambda, and I'm using httpx with asyncio. I'm running into an issue when using the code from the python REPL whereby re-running the
lambda_handler
function after successfully running it leads to a "RuntimeError: Event loop is closed" exception. I believe this is due to following the "single global client instance" from the note in the docs while callingasyncio.run(main())
from the handler.Is this expected behavior? I was hoping to follow the pattern of having a global client instance so that connection pools would be re-used across multiple requests while the lambda execution environment was warm. I've used this same pattern successfully in sync code without issue.
If I instead create the client inside
async def main()
each time, the error no longer occurs within the REPL.Minimal reproduction:
HTTPX version:
httpx = "0.27.0"
Results running in the REPL:
expand for shell output
Beta Was this translation helpful? Give feedback.
All reactions