Skip to content

Significant Performance Degradation with Frequent Recreation of httpx.Client Instances #3251

Closed Answered by deedy5
deedy5 asked this question in Potential Issue
Discussion options

You must be logged in to vote

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.

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")

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by deedy5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant