Asyncio -- delayed calculation

Chris Angelico rosuav at gmail.com
Mon Nov 28 08:03:58 EST 2016


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()

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

There may be other ways, but that's the best I could find. It seems to
do what you want.

ChrisA



More information about the Python-list mailing list