【问题标题】:Is it possible to use multiprocessing.Event to implement a synchronization barrier for pool of processes?是否可以使用 multiprocessing.Event 为进程池实现同步屏障?
【发布时间】:2013-04-25 22:00:32
【问题描述】:

是否可以使用 multiprocessing.Event 为进程池实现同步屏障?我正在使用 Python 2.7。但看起来事件没有在进程之间共享。 我的代码有问题吗?

def test_func(event):
    event.wait()
    return datetime.datetime.now()

def log_result(result):
    result_list.append(result)

if __name__ == '__main__':
    pool_size=10
    pool = multiprocessing.Pool(processes=pool_size)
    event = multiprocessing.Manager().Event()
    for _ in xrange(pool_size):
        pool.apply_async(test_func, args=(event,), callback=log_result)
    pool.close()
    pool.join()   
    time.sleep(5)
    event.set()

【问题讨论】:

    标签: python multiprocessing


    【解决方案1】:

    你有死锁:pool.join() 等待启动的进程,所有进程都在 event.wait() 上等待。加入 Sedd 池:

    加入()

    Wait for the worker processes to exit
    

    基本上你必须在“event.set()”之后移动“pool.join()”,一切都应该没问题。

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 2021-05-14
      • 2015-01-04
      • 2011-12-22
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多