Concurrency in Python: Understanding Asyncio and Futures

Mar 24, 2024 ยท 2 min read

Python provides powerful tools for handling concurrency and parallelism. Two key concepts are asyncio and futures. Both help manage non-blocking operations, but they serve different purposes.

Asyncio: Asynchronous I/O

The asyncio module enables writing asynchronous code using the async/await syntax. It allows performing I/O-bound operations without blocking the main thread. Some examples:

import asyncio

async def fetch_data():
    # Perform some async I/O operation
    data = await some_async_fetch() 
    return processed_data

async def main():
    result = await fetch_data()
    print(result)

asyncio.run(main())

Asyncio works by suspending and resuming coroutines instead of using OS threads. This makes it very efficient for tasks like network requests and file I/O.

The event loop handles switching between coroutines when they are suspended. So asyncio enables concurrency but not parallelism - only one coroutine runs at a time.

Futures: Managing Concurrency

A future represents an asynchronous operation that may complete at some point. They allow scheduling work to be done in separate threads or processes:

from concurrent.futures import ThreadPoolExecutor

def blocking_operation(a, b):
    # Returns after some computations
    return a + b

with ThreadPoolExecutor() as executor:
    f = executor.submit(blocking_operation, 1, 2)
    print(f.result()) 

Futures abstract away thread/process details. They provide a clean API for managing and coordinating concurrent work.

So asyncio enables asynchronous I/O handling in a single thread, while futures handle parallelism across threads/processes. Together they provide powerful concurrency options.

The key difference is that asyncio avoids threads altogether, while futures use threads/processes under the hood. Asyncio works at a lower level to minimize overhead.

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: