Reduce timeouts at scale
The DashScope SDK supports connection reuse to reduce resource consumption and improve efficiency.
Connection pooling is enabled by default. Adjust the following parameters as needed.
Configure connection pool parameters and call a model service:
The Python SDK supports connection reuse via a custom Session. Two methods are available: HTTP asynchronous and HTTP synchronous.
Use
Configure async connection reuse and call a model service:
Use
Configure sync connection reuse and call a model service:
Reuse the same Session across multiple calls:
- Java SDK: Connection pooling is enabled by default. Configure max connections and timeouts as needed.
- Python SDK: Pass a custom Session to enable connection reuse. Supports sync and async calls.
Java SDK
Connection pooling is enabled by default. Adjust the following parameters as needed.
Parameters
| Parameter | Description | Default | Unit | Notes |
|---|---|---|---|---|
connectTimeout | Timeout for establishing a connection. | 120 | seconds | Shorter timeouts reduce wait time in low-latency scenarios. |
readTimeout | Timeout for reading data. | 300 | seconds | |
writeTimeout | Timeout for writing data. | 60 | seconds | |
connectionIdleTimeout | Timeout for idle connections. | 300 | seconds | Longer idle timeouts avoid frequent reconnections under high concurrency. |
connectionPoolSize | Maximum connections in the pool. | 32 | items | Too few connections cause blocking; too many increase server load. |
maximumAsyncRequests | Maximum concurrent requests across all hosts. Must be ≤ connectionPoolSize. | 32 | requests | |
maximumAsyncRequestsPerHost | Maximum concurrent requests per host. Must be ≤ maximumAsyncRequests. | 32 | items |
Code example
Before running the code, get an API key and export it as an environment variable. Then install the latest SDK.
Python SDK
The Python SDK supports connection reuse via a custom Session. Two methods are available: HTTP asynchronous and HTTP synchronous.
HTTP asynchronous
Use aiohttp.ClientSession with aiohttp.TCPConnector for async connection reuse.
| Parameter | Description | Default | Notes |
|---|---|---|---|
limit | Total connection limit | 100 | Higher values improve concurrency. |
limit_per_host | Connection limit per host | 0 (unlimited) | Prevents excessive load on a single host. |
ssl | SSL context configuration | None | SSL certificate validation for HTTPS connections. |
Code example
Before running the code, get an API key and export it as an environment variable. Then install the latest SDK.
HTTP synchronous
Use requests.Session for sync connection reuse. Requests within the same Session reuse the TCP connection.
Code example
Before running the code, get an API key and export it as an environment variable. Then install the latest SDK.
Best practices
- Java SDK: Set
connectionPoolSizeandmaximumAsyncRequestsbased on your concurrent workload. Too few connections cause blocking; too many increase load. - Python SDK: Use
withstatements to manage the Session lifecycle and ensure proper resource cleanup. - Choose the right method: Use async calls for async applications (like asyncio or FastAPI). Use sync calls for traditional applications.