python AsyncResult.successful() 不 return

python AsyncResult.successful() does not return

我正在尝试 python 多处理。我想要几个相互独立的进程并行 运行,因为它们 return 使用 ApplyAsync.successful() 实用程序检查进程是否成功。但是,当我在子进程的回调中调用成功时,脚本挂起。

import multiprocessing as mp
import time

result_map = {}

def foo_pool(x):
    time.sleep(2)
    print x
    return x


result_list = []
def log_result(result):
    print result_map[result].successful()    #hangs                                                                                            
    result_list.append(result)


def apply_async_with_callback():
    pool = mp.Pool()
    for i in range(10):
        result_map[i] = pool.apply_async(foo_pool, args = (i, ),    callback = log_result)

    pool.close()
    pool.join()

    print(result_list)



if __name__ == '__main__':
    apply_async_with_callback()

不需要检查successful(),因为只有在结果成功时才会调用回调。

相关代码如下(multiprocessing/pool.py - AsyncResult)

def _set(self, i, obj):
    self._success, self._value = obj
    if self._callback and self._success:  # <-----
        self._callback(self._value)       # <-----
    self._cond.acquire()
    try:
        self._ready = True
        self._cond.notify()
    finally:
        self._cond.release()
    del self._cache[self._job]