Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asyncio print status of coroutines progress

I have a bunch of coroutines that doing some work

@asyncio.coroutine
def do_work():
    global COUNTER
    result = ...
    if result.status == 'OK':
        COUNTER += 1

and another one

COUNTER = 0
@asyncio.coroutine
def display_status():
    while True:
        print(COUNTER)
        yield from asyncio.sleep(1)

which have to display how many coroutines have finished their work. How to properly implement this task? Following solution doesn't work

@asyncio.coroutine
def spawn_jobs():
    coros = []
    for i in range(10):
        coros.append(asyncio.Task(do_work()))
    yield from asyncio.gather(*coros)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.create_task(display_status())
    loop.run_until_complete(spawn_jobs())
    loop.close()

I expect that counter will be printed to the console every second no matter what do_work() coroutines do. But I have just two outputs: 0 and after a few seconds repeating 10.

like image 906
Most Wanted Avatar asked Jul 10 '15 14:07

Most Wanted


1 Answers

But I have just two outputs: 0 and after a few seconds repeating 10.

I can't reproduce it. If I use:

import asyncio
import random

@asyncio.coroutine
def do_work():
    global COUNTER
    yield from asyncio.sleep(random.randint(1, 5))
    COUNTER += 1

I get the output like this:

0
0
4
6
8
Task was destroyed but it is pending!
task: <Task pending coro=<display_status() running at most_wanted.py:16> wait_for=<Future pending cb=[Task._wakeup()] created at ../Lib/asyncio/tasks.py:490> created at most_wanted.py:27>

The infinite loop in display_status() causes the warning at the end. To avoid the warning; exit the loop when all tasks in the batch are done:

#!/usr/bin/env python3
import asyncio
import random
from contextlib import closing
from itertools import cycle

class Batch:
    def __init__(self, n):
        self.total = n
        self.done = 0

    async def run(self):
        await asyncio.wait([batch.do_work() for _ in range(batch.total)])

    def running(self):
        return self.done < self.total

    async def do_work(self):
        await asyncio.sleep(random.randint(1, 5)) # do some work here
        self.done += 1

    async def display_status(self):
        while self.running():
            await asyncio.sleep(1)
            print('\rdone:', self.done)

    async def display_spinner(self, char=cycle('/|\-')):
        while self.running():
            print('\r' + next(char), flush=True, end='')
            await asyncio.sleep(.3)

with closing(asyncio.get_event_loop()) as loop:
    batch = Batch(10)
    loop.run_until_complete(asyncio.wait([
        batch.run(), batch.display_status(), batch.display_spinner()]))

Output

done: 0
done: 2
done: 3
done: 4
done: 10
like image 128
jfs Avatar answered Oct 23 '22 02:10

jfs