Skip to main content
By using asyncio and the aiohttp library, you can send multiple requests simultaneously, significantly reducing wait time and improving response time for batch operations.
Efficiency and ScalabilityAsynchronous requests enable non-blocking operations, making your application more efficient and scalable, especially when making multiple simultaneous API calls.

Prerequisites

  • Ensure you have the aiohttp library installed: pip install aiohttp
  • An API key stored securely, preferably in a PARADIGM_API_KEY environment variable.

Asynchronous Programming with asyncio

asyncio is a Python library that provides a framework for writing concurrent code using the async and await syntax. It is particularly useful for high-level structured network code and I/O-bound operations. You can find more information about Asyncio here.

Configuring the Asynchronous HTTP Client

To use asynchronous features, use aiohttp.ClientSession. Initialize the session with your API key and the appropriate endpoint.

Asynchronous API Calls

Here’s how to implement asynchronous API calls:
Create an async function that sends a message to the API and awaits the response.
Use asyncio.gather to send multiple requests simultaneously. This function waits for all futures (asynchronous operations) to complete.
Use asyncio.run() to execute the main function, which handles all asynchronous operations.

Complete Example

Here is the complete example code:

Comparison with Synchronous Execution

When comparing asynchronous execution with traditional synchronous (sequential) execution, asynchronous operations generally complete in much less time, with potential for even greater improvement depending on the length of different requests. This is particularly true for I/O-bound tasks like API requests. The efficiency gains from asynchronous execution come from its non-blocking nature, which allows other tasks to continue without waiting for I/O operations to complete.
Best Practices
  • Always use await with async functions to avoid runtime errors.
  • Reuse the same ClientSession for multiple requests to improve performance.
  • Always close the session properly using the async with context manager.
  • For Jupyter notebooks, run asynchronous code via a separate Python script using the magic command !python file_to_execute.py in a cell to avoid event loop issues.
By incorporating asynchronous requests into your application, you can achieve greater efficiency and scalability, particularly when handling a large number of API calls.

Complete Comparison Example

To compare synchronous and asynchronous API calls in a practical scenario, you can use the following snippet. This snippet will create a Python file, speed_test.py, implementing synchronous and asynchronous API requests, respectively. You can then run this script to observe the difference in execution time, demonstrating the efficiency of asynchronous programming for batch requests.
speed_test.py
To run the comparison:
  1. Execute the code snippet above in a Jupyter notebook cell to create speed_test.py.
  2. Run the script in the Jupyter notebook or a terminal using the command !python speed_test.py.
This script will first run the asynchronous version, displaying the total execution time. It will then run the synchronous version, doing the same. Comparing the two execution times will illustrate the efficiency gains achievable with asynchronous API calls. In our case, we obtained the following output:

Error Handling and Timeout

It’s important to add robust error handling for asynchronous API calls:

Conclusion

Leveraging asynchronous API requests via aiohttp can significantly improve application performance and scalability. As demonstrated, asynchronous execution can be nearly 5 times faster than synchronous methods, offering significant efficiency gains. This approach is essential for handling high-volume API interactions, ensuring application efficiency.