gpt4 book ai didi

python - `as_completed`模块中的模拟 `multiprocessing`

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:03 26 4
gpt4 key购买 nike

我正在 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

关于python - `as_completed`模块中的模拟 `multiprocessing`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911361/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com