Significant Performance Degradation with Frequent Recreation of httpx.Client
Instances
#3251
-
We've conducted extensive benchmark tests comparing the performance of several HTTP clients ( During our tests, we noticed a significant performance degradation when using To illustrate this issue, we performed a series of tests making 2000 requests to a local server with response sizes varying between 5KB, 50KB, and 200KB. We measured both wall clock time and CPU time for each client. The results consistently showed that Additional Resources:
We believe this observation could be valuable for the We kindly ask the Thank you for your attention to this matter. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In case anyone is wondering. By default httpx.CLient() creates a new ssl context every initialization, that's why it's so slow. import httpx
import ssl
from timeit import timeit
ssl_context = ssl.create_default_context()
# Default client
default_time = timeit(lambda: httpx.Client(), number=1000)
print(f"Time taken to create default client 1000 times: {default_time} seconds")
# Client with ssl context
ssl_time = timeit(lambda: httpx.Client(verify=ssl_context), number=1000)
print(f"Time taken to create client with SSL context 1000 times: {ssl_time} seconds") |
Beta Was this translation helpful? Give feedback.
In case anyone is wondering.
By default httpx.CLient() creates a new ssl context every initialization, that's why it's so slow.
To speed things up, just create an ssl context and reuse it.