Py3 ThreadPoolExecutor:如何在执行前获取剩余项目数

Py3 ThreadPoolExecutor: how to get the numof remaining items until execution

我正在尝试向 Python3 中的 ThreadPool 添加多个长 运行 线程。 对于每个线程,在执行之前如何确定它前面有多少任务?

我的目标是向用户展示 "You have X items pending before your task"。 到max_workers.

时不精确也不是问题
with futures.ThreadPoolExecutor(max_workers=2) as executor:
  future1 = executor.submit(task, 10) 
  future2 = executor.submit(task, 10)
  future3 = executor.submit(task, 10)

 # my naive failed attempt was
 numOfRemainingTasks = (len(executor)-1) - executor.indexof(future3)

如果我没理解错的话,你想要类似的东西吗?

import concurrent.futures

class CustomExecutor(concurrent.futures.ThreadPoolExecutor):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
    def submit(self, *args, **kwargs):
        print(f'There are {self._work_queue.qsize()} items in front of me')
        return super().submit(*args, **kwargs)

def task(num):
    return num + 10


with CustomExecutor(max_workers=2) as executor:
    futures = (executor.submit(task, 10) for _ in range(6))
    for future in concurrent.futures.as_completed(futures):
        print(future.result())

There are 0 items in front of me
There are 0 items in front of me
There are 0 items in front of me
There are 1 items in front of me
There are 1 items in front of me
There are 2 items in front of me
....