Sobes.tech
Junior — Middle

How many threads and processes are involved when using asyncio in Python?

sobes.tech AI

Answer from AI

In asyncio in Python, one thread and one process are used by default. Asynchronous programming in asyncio is based on an event loop that manages the execution of coroutines within a single thread, switching between them without creating new threads or processes.

If you need to use multiple threads or processes, this is explicitly done through the threading or multiprocessing modules, but asyncio itself does not create them automatically.

Here's an example of simple asyncio code running in a single thread:

import asyncio

async def say_hello():
    print("Hello")
    await asyncio.sleep(1)
    print("World")

asyncio.run(say_hello())

All the code here runs in a single thread and process.