【问题标题】:Get progress in ThreadPoolExecutor在 ThreadPoolExecutor 中获取进度
【发布时间】:2019-07-03 13:50:17
【问题描述】:

我正在使用 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]

有没有办法获得已完成任务的数量?

谢谢

【问题讨论】:

    标签: python multithreading python-3.6 python-asyncio


    【解决方案1】:

    我发现了一些东西,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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 2017-06-23
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多