Получить прогресс в ThreadPoolExecutor - PullRequest
0 голосов
/ 03 июля 2019

Я использую asyncio (в Python 3.6) для планирования нескольких асинхронных задач.

В следующем примере:

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]

Есть ли способ получить количество выполненных задач?

Спасибо

1 Ответ

0 голосов
/ 03 июля 2019

Я нашел что-то, у asyncio Future объекта есть функция done ():

from asyncio import Future
from typing import List

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