How asyncio works in Python?

Mar 24, 2024 ยท 2 min read

Python's asyncio module provides infrastructure for writing concurrent code using the async/await syntax. Asyncio allows you to execute code out of order while waiting on long-running tasks like network requests without blocking the rest of the program.

The Event Loop

At the heart of asyncio is the event loop. This keeps track of tasks, executes them when conditions are met, and puts pending tasks to sleep:

import asyncio

async def my_coro():
    print('Hello world!')

loop = asyncio.get_event_loop()
loop.run_until_complete(my_coro())
loop.close()

The event loop runs the my_coro() coroutine to completion before closing.

Async Functions and Awaitables

Functions declared with async are awaitables - objects that can be awaited on:

import asyncio

async def my_coro():
    await some_long_running_task()
    print('Done!')

loop = asyncio.get_event_loop()
loop.run_until_complete(my_coro())

While waiting on some_long_running_task, other events can be processed.

You can await on things like network requests to suspend execution without blocking:

resp = await aiohttp.get('http://example.com')
print(resp)

Concurrent Execution with gather()

asyncio.gather() runs awaitables like coroutines concurrently:

await asyncio.gather(
    coro_1(), 
    coro_2(),
)

The event loop interleaves execution of coro_1 and coro_2 allowing them to run simultaneously.

Key Takeaways

  • Asyncio provides infrastructure for async/await through the event loop
  • async functions are awaitables that can be awaited on
  • asyncio.gather() runs awaitables concurrently
  • Awaiting suspends execution without blocking, enabling asynchrony
  • With these building blocks, you can write highly concurrent Python without threads or callbacks!

    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: