Asyncio -- delayed calculation

Steve D'Aprano steve+python at
Fri Dec 2 08:59:07 EST 2016

On Thu, 1 Dec 2016 06:53 pm, Christian Gollwitzer wrote:

> well that works - but I think it it is possible to explain it, without
> actually understanding what it does behind the scences:
> x = foo()
> # schedule foo for execution, i.e. put it on a TODO list
> await x
> # run the TODO list until foo has finished

Nope, sorry, that doesn't help even a little bit. For starters, it doesn't
work: it is a syntax error.

py> async def foo():
...     return 1
py> x = foo()
py> await x
  File "<stdin>", line 1
    await x
SyntaxError: invalid syntax

But even if it did work, why am I waiting for the TODO list to finish?
Doesn't that mean I'm now blocking, which goes completely against the idea
of writing non-blocking asynchronous code? If I wanted to block waiting for
x, I'd just make it a regular, easy-to-understand, synchronous function.

Besides, where does x stash it's result? In a global variable? What if
another async routine messes with the same global?

My first impressions on this is that we have a couple of good models for
preemptive parallelism, threads and processes, both of which can do
everything that concurrency can do, and more, and both of which are
significantly easier to understand too.

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

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list