多处理模块中的模拟as_completed

时间:2018-08-18 18:08:09

标签: python multiprocessing

我正在Python 2.7模块as_completed中寻找concurrent.futures函数的类似物(来自Python 3 multiprocessing)。我当前的解决方案:

import time
from multiprocessing import Pool
def f(x):
    time.sleep(x)
    return x
if __name__ == '__main__':
    pool = Pool()
    a = pool.apply_async(f, [4])
    b = pool.apply_async(f, [2])
    while any([a,b]):
        if a and a.ready(): print a.get(); a=False 
        if b and b.ready(): print b.get(); b=False

1 个答案:

答案 0 :(得分:1)

一种快速而肮脏的方法是将异步结果对象存储在一个可迭代的状态中,并定期轮询其状态。

from multiprocessing import Pool
from random import random
from time import sleep


def wrapped_sleep(n, i):
    sleep(n)
    return n, i

if __name__ == '__main__':
    pool = Pool()
    random_sleep_durations = [random() * 10 for _ in xrange(100)]
    results = [
        pool.apply_async(wrapped_sleep, (n, i, ))
        for i, n in enumerate(random_sleep_durations)
    ]

    while results:
        sleep(0.1)
        mature_indices = []
        mature_results = []

        for i, candidate in enumerate(results):
            if candidate.ready():
                mature_indices.append(i)
                break

        for i in mature_indices:
            mature_results.append(results.pop(i).get())

        for result in mature_results:
            print result