Warm tip: This article is reproduced from stackoverflow.com, please click
multithreading python python-3.6 python-asyncio

Get progress in ThreadPoolExecutor

发布于 2020-03-27 10:19:35

I'm using asyncio (in python 3.6) to schedule multiple asynchronous tasks.

On the following example:

import concurrent.futures
import time
import asyncio

def long_task(t):
    print(t)
    time.sleep(1)
    return t

loop = asyncio.get_event_loop()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
inputs = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
futures = [loop.run_in_executor(executor, long_task, i) for i in inputs]

Is there a way to get the number of finished tasks ?

Thanks

Questioner
RobinFrcd
Viewed
79
RobinFrcd 2019-07-03 22:51

I found something, asyncio Future object have a done() function:

from asyncio import Future
from typing import List

def get_progress(futures: List[Future]) -> int:
    return sum([f.done() for f in futures])