0

I'm trying to understand how asyncio allows for multiple IO waits to happen in parallel. It is often said that asyncio enables concurrency, but not parallelism. From what I understand, this is true for Python code as such, but something must be happening in parallel, otherwise the execution time would still be the sum of its parts? That something is presumably waiting for IO.

The generic example uses sleep - we have two tasks sleeping, at least conceptually, in parallel.

Other tutorials use aiohttp to run multiple HTTP requests concurrently, presumably with many of them waiting on their data in parallel. But how is it done? How is a (blocking) read initiated and then control relinquished until it's done? I imagine the actual parallelism happens at the OS level, or maybe some underying C library, but I don't know how to express that in Python.

What happens under the hood (e.g. in aiohttp)?

Błażej Czapp
  • 2,478
  • 2
  • 24
  • 18
  • 2
    This is a great talk: https://youtu.be/MCs5OvhV9S4 – mkrieger1 Jul 26 '20 at 11:53
  • 1
    +1 for the talk linked by mkrieger1. A good rule of thumb is that "threads are for working in parallel, and async is for waiting in parallel". Asyncio funnels all the individual awaits initiated by coroutines to a single OS-level poll that waits for a lot of stuff in parallel. Whenever something happens, asyncio dispatches the event by resuming the coroutine that registered interest in that event in its "await". – user4815162342 Jul 26 '20 at 11:58

0 Answers0