Asyncio -- delayed calculation

Marko Rauhamaa marko at pacujo.net
Fri Dec 2 09:19:29 EST 2016


Steve D'Aprano <steve+python at pearwood.info>:

> py> await x
>   File "<stdin>", line 1
>     await x
>           ^
> SyntaxError: invalid syntax

"await" is only allowed inside a coroutine.

> So why do we need asyncio? What is it actually good for?

Asyncio is a form of cooperative multitasking. It presents a framework
of "fake threads". The objective and programming model is identical to
that of threads.

As to why Python should need a coroutine framework, you can answer it
from two angles:

  1. Why does one need cooperative multitasking?

  2. Why should one use coroutines to implement cooperative
     multitasking?


1. Cooperative multitasking has made its way in Java ("NIO") and C#
(async/await). It has come about as enterprise computing realized the
multithreading model of the 1990's was shortsighted. In particular, it
wasn't scalable. Enterprise solutions collapsed under the weight of tens
of thousands of threads. Stack space ran out and schedulers became slow.
<URL: https://en.wikipedia.org/wiki/C10k_problem>

2. I have always been into asynchronous programming (cooperative
multitasking), but coroutines are far from my favorite programming
model. I am guessing Guido introduced them to Python because:

 * C# has them (me too!).

 * They have a glorious computer scientific past (CSP, emperor's new I/O
   framework).

 * They look like threads.

 * They were already there in the form of generators (low-hanging
   fruit).

And, maybe most importantly:

 * Twisted (et al) had needed an event-driven framework but Python
   didn't have one out of the box (<URL:
   https://lwn.net/Articles/692254/>).


Marko



More information about the Python-list mailing list