Does asyncio run in single thread python ?

Mar 17, 2024 ยท 2 min read

Python's asyncio module allows you to write concurrent code using a single-threaded event loop model. This provides performance benefits compared to traditional threading or multiprocessing approaches in Python.

How Asyncio Achieves Concurrency

The asyncio event loop runs in a single thread in the Python process. All asyncio coroutines and tasks get executed within this event loop sequentially. However, when a coroutine awaits on an I/O event like a network request, instead of blocking the thread, the event loop can switch context and run another coroutine while waiting.

import asyncio

async def fetch_data():
    print('start fetching')
    await asyncio.sleep(2) # simulated I/O wait
    print('done fetching')

async def print_numbers():
    for i in range(10):
        print(i)
        await asyncio.sleep(0.25)

async def main():
    task1 = asyncio.create_task(fetch_data())
    task2 = asyncio.create_task(print_numbers())

    await task1
    await task2

asyncio.run(main())

This allows asyncio to achieve concurrent execution and high throughput on a single thread. So while the asyncio event loop itself runs on a single thread, it can coordinate multiple coroutines by leveraging asynchronous I/O and cooperative multitasking.

When to Use Asyncio

Asyncio works great for I/O bound workloads like web servers handling concurrent requests or network applications. It can achieve high throughput when there are frequent waits for I/O allowing other coroutines to run.

However for CPU heavy tasks like scientific computing, asyncio provides little benefit over threading or multiprocessing.

So in summary, asyncio provides a performant single-threaded concurrent model in Python by smartly managing I/O waiting periods. It is well suited for I/O bound use cases instead of CPU bound workloads.

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: