Asyncio -- delayed calculation

Steve D'Aprano steve+python at pearwood.info
Mon Nov 28 21:23:46 EST 2016


On Tue, 29 Nov 2016 12:03 am, Chris Angelico wrote:

> On Mon, Nov 28, 2016 at 11:48 PM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>> When I try running that, I get no output. No error, no exception, the
>> run_until_complete simply returns instantly.
> 
> When I do, I get this warning:
> 
> asynctest.py:17: RuntimeWarning: coroutine 'Counter.count_down' was
> never awaited
>   obj.count_down()

Ah yes, I saw that earlier, from an earlier version of the code that failed
with an exception. But it was enough to raise the warning, which then was
suppressed.


> Putting an 'await' in front of that call causes the tasks to be run
> consecutively, of course. The most similar code for running tasks
> concurrently seems to be this:
> 
> async def main():
>     pool = [Counter() for i in range(5)]
>     await asyncio.gather(*(obj.count_down() for obj in pool))
> 
> Taken from:
>
https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks

This is confusing: why is this awaiting something inside an async function?
Doesn't that mean that the await asyncio.gather(...) call is turned
blocking?



-- 
Steve
“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