Repository navigation
Client / AsyncClient instantiated extremely slow #3535
Replies: 1 comment
|
There is setup work before the first request: constructing the default transport creates an SSL context and loads trust information. Environment-derived proxy transports can add setup too. Your timings alone do not identify which part dominates on your machine, but they do measure more than entering an empty async context manager. Keeping one client for the logical workload amortizes both that construction cost and connection setup. If you genuinely need several clients with separate cookies/authentication, you can additionally reuse a configured SSL context: import ssl
import certifi
import httpx
# Once at application startup; configure the CA policy your deployment needs.
tls = ssl.create_default_context(cafile=certifi.where())
async def fetch_statuses(urls):
async with httpx.AsyncClient(verify=tls) as client:
statuses = []
for url in urls:
response = await client.get(url)
statuses.append(response.status_code)
return statusesSharing For profiling, compare client construction separately from the first request, and compare the default SSL context configuration and async client lifetime guidance. AI-assisted; checked SSL-context reuse in the construction path and the single-client/multiple-request example locally on HTTPX 0.28.1. No universal constructor timing is claimed. |
Uh oh!
There was an error while loading. Please reload this page.
Client / AsyncClient instantiated extremely slow (over 200ms).
The examples are making only one request by Client, this may cause misleading.
Though there were docs about clients, developers did not know that not only they can't get the most benefit from connection pooling, BUT ALSO face over 200ms cost to create client itself. TUN Proxy will aggravate the condition by 2 ~ 3 times.
Code:
Results:
All reactions