是否可以使用concurrent.futures.ThreadPoolExecutor动态添加新作业?

时间:2017-12-19 11:24:18

标签: python concurrent.futures

我想到的用例如下:我想用 ThreadPoolExecutor 启动一系列作业,然后当作业完成时,我想在队列中添加一个新作业。我还想知道下一个工作何时完成并重复上述步骤。在我有机会观察到预定义数量的结果后,我想正确终止所有事情。例如,请考虑以下代码,其中 run_con 方法中的注释位显示了我想要实现的内容。

import numpy as np
import time
from concurrent import futures
MAX_WORKERS = 20
seed = 1234
np.random.seed(seed)
MSG = "Wall time: {:.2f}s"

def expensive_function(x):
    time.sleep(x)
    return x

def run_con(func, ts):
    t0 = time.time()
    workers = min(MAX_WORKERS, len(ts))
    with futures.ThreadPoolExecutor(workers) as executor:
        jobs = []
        for t in ts:
            jobs.append(executor.submit(func, t))
        done = futures.as_completed(jobs)
        for future in done:
            print("Job complete: ", future.result())
            # depending on some condition, add new job to jobs, e.g.
            # jobs.append(func, np.random.random())
            # update done generator
            # if a threshold on total jobs is reached, close every thing down sensibly.
    print(MSG.format(time.time()-t0))

ts = np.random.random(10)*5
print("Sleep times: ", ts)
run_con(expensive_function, ts)

这可以通过concurrent.futures实现吗?如果没有,有哪些替代方案?

0 个答案:

没有答案