Replies: 2 comments 3 replies
-
Possibly depends on how/where you're instantiating You only want to use a context manager if you're being precise about the expected lifetime of the client connection pool. In the sync case most of the time you actually just want a single global client instance shared across the entire codebase. The connection pool will be closed properly on shutdown. If you're in a context where there is a clean startup/shutdown sequence that you can hook into, then it's a little neater to be precise about the client lifespan, and pass the instance around wherever it's needed. We could probably do with some docs on recommended client usage centred around the client lifespan. |
Beta Was this translation helpful? Give feedback.
-
It's not a dumb question in the slightest.
Yes, the client will persist between requests.
The short answer is... unless you've got a good reason to do something different, then just use a single client instance, and don't bother with the context manager. (You really only need it in the async case.) |
Beta Was this translation helpful? Give feedback.
-
Hello!
I am using the httpx client through context manager inside a class for some internal service in Django, and after a large number of requests I am getting '[Errno 9] Bad file descriptor'. I suspect that for some reason the connections after being closed are not being cleaned from system for some reason, but this is a wild guess from someone not really experienced in this part.
Any help or ideas are appreciated. I am not sure if this is the correct way to use inside a class with the context manager.
Beta Was this translation helpful? Give feedback.
All reactions