我正在 Python 2.7 模块 multiprocessing 中寻找 as_completed 函数(来自 Python 3 concurrent.futures)的模拟。我目前的解决方案:
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
请您参考如下方法:
一种快速而肮脏的方法是将异步结果对象存储在一个可迭代的对象中,并定期轮询它们的状态。
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
