asyncio with aiohttp to run thousands of concurrent calls against KiosAPI from a single script. This approach gives you connection pooling, fine-grained flow control, and the throughput needed for demanding pipelines.
Installation
Install the required packages:Full Example
The example below demonstrates the recommended pattern: a sharedClientSession for connection pooling, a Semaphore to cap in-flight requests, and a while True loop that continuously drains your prompt source until it is exhausted.
Key Design Points
Three architectural decisions make this pattern reliable at scale:
- Single shared
ClientSession— create oneaiohttp.ClientSessionand reuse it for every request. This enables HTTP connection pooling and dramatically reduces per-request overhead compared to opening a new session each time. Semaphorefor concurrency control — caps the number of in-flight requests atMAX_CONCURRENCY, preventing resource exhaustion on both the client and the server.while Trueloop — continuously pulls the next batch of prompts until your source is exhausted. Replacegenerate_prompts()with your own logic: a file reader, message queue consumer, or any other iterable source.
Limits and Best Practices
Error Handling
At high concurrency, individual request failures are expected. Always passreturn_exceptions=True to asyncio.gather and implement retries with exponential backoff for transient errors such as 429, 500, 502, and 503.