Making Async HTTP Requests in Python with requests and asyncio

Feb 3, 2024 ยท 2 min read

The Python requests library provides a simple API for making HTTP requests. By default, requests makes synchronous (blocking) requests, meaning your code waits for the response before continuing.

However, Python also includes the asyncio module for asynchronous programming. By combining requests and asyncio, you can make non-blocking HTTP requests in Python.

Using asyncio and aiohttp for Asynchronous Requests

The easiest way to make async requests is to use the aiohttp library, which builds on top of asyncio:

import asyncio
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://example.com') as response:
            print(response.status)
            print(await response.text())

asyncio.run(main())

This performs the HTTP request in a non-blocking way, allowing other processing to happen while waiting for the response.

Making requests Async with grequests

Alternatively, you can use the grequests library to make the familiar requests API asynchronous:

import grequests

urls = [
    'http://example.com/1',
    'http://example.com/2',
    'http://example.com/3'
]

rs = (grequests.get(u) for u in urls)
responses = grequests.map(rs)

for response in responses:
    print(response.text)

The key thing is that grequests uses asyncio under the hood to enable concurrent requests.

Considerations

The async approach works best for I/O bound workloads involving network requests or file operations. For CPU heavy processing, async may not help much.

Also, you need to ensure your code properly waits for all async operations to complete before continuing. Proper use of await and async with helps manage this.

Overall, asyncio is a powerful tool for making I/O heavy work in Python more efficient and scalable through asynchronous, non-blocking patterns.

Browse by tags:

Browse by language:

Tired of getting blocked while scraping the web?

ProxiesAPI handles headless browsers and rotates proxies for you.
Get access to 1,000 free API credits, no credit card required!