Asyncio -- delayed calculation

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

Steve D'Aprano <steve+python at>:

> 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

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.

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

 * They look like threads.

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

And, maybe most importantly:

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


More information about the Python-list mailing list