Maybe document when context manager shouldn't be used #2444
-
|
Quite recently we started using FastAPI to develop small async-capable apis, and whenever we need to invoke http requests from an async endpoint we use httpx. For a while we just followed the documentation and dropped In our code. However, in some of our apis we saw noticeable slowdown/excessive cpu usage and we (after much troubleshooting) figured out that this was a service that performs a lot of http requests to another service. From there we learned that instantiating httpx using a context manager as shown above has significant performance implications. After learning that, we started instatiating a "global" httpx client, and used that throughout the code instead. The results were night and day. I'm wondering if the documentation should be more nuanced around this, and maybe disuss situations where the context manager shouldn't be used but one should instead instantiate a more long-lived client and reuse that. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Hi @trondhindenes. I've attempted to give an answer to this here... #2191 I'm a bit uncertain of how we ought to update the documentation. I'd be happier if we were able to recommend lifespan based clients, rather than global clients, particularly in the async case, but. 🤷♂️ |
Beta Was this translation helpful? Give feedback.
Hi @trondhindenes.
I've attempted to give an answer to this here... #2191
I'm a bit uncertain of how we ought to update the documentation. I'd be happier if we were able to recommend lifespan based clients, rather than global clients, particularly in the async case, but. 🤷♂️