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:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: