“multiprocessing”modu中的“as\u completed”模拟

2024-04-26 13:19:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找python2.7模块multiprocessing中的as_completed函数的模拟函数(来自python3concurrent.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

Tags: 模块and函数importfalsegetasyncif
1条回答
网友
1楼 · 发布于 2024-04-26 13:19:41

一种快速而肮脏的方法是将异步结果对象存储在iterable中,并定期轮询它们的状态。你知道吗

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

相关问题 更多 >