【问题标题】:multiprocessing.pool does not terminate (python 2.7)multiprocessing.pool 不会终止(python 2.7)
【发布时间】:2018-01-13 02:38:01
【问题描述】:

我对 multiprocessing.pool 有疑问。即使满足下面 while 循环中定义的退出条件,我的工作人员也永远不会退出。在最后一个工作人员完成其工作后,不再进入 while 循环。但是,子进程不会像我预期的那样终止,而只是空闲并且主进程不会继续。这是在 Ubuntu 上。

最后一个输出是“完成”,之后什么也没有发生。如果我添加我在下面注释掉的行,即handler.get(),程序将运行并正确终止而没有错误(仅使用一个进程)。也许这里有一个明显的错误,但我没有想法,感谢任何帮助!

manager = multiprocessing.Manager()
pool = multiprocessing.Pool()
queue = manager.Queue()
lock = manager.Lock()

finished = manager.list()
active = manager.list()
pending = manager.list()

for core in core_list:
     queue.put(core)
     pending.put(core.id)

while len(pending) > 0:
    print "Submit jobs"
    core = queue.get(block=True)
    handler = pool.apply_async(solve_core, (core, core_list, params))
    #handler.get()

pool.close()
pool.join()

def solve_core(core, core_list, params):
    lock.acquire()
    pending.remove(core.id)
    active.append(core.id)
    lock.release()

    # Process some data...

    lock.acquire()
    active.remove(core.id)
    finished.append(core.id)
    for new_core in core_list:
        if some_condition:
            queue.put(new_core)
            pending.append(new_core.id)
    lock.release()
    print "Done"

【问题讨论】:

  • 您的代码示例无法运行,其他人如何为您调试?
  • 我希望/假设存在结构错误,这对于有更多经验的人来说是显而易见的。显然这是真的!

标签: python python-2.7 multiprocessing python-multiprocessing terminate


【解决方案1】:

有一个明显的竞争条件错误,尽管还有更多。 您的程序依赖子进程来清空pending 列表,但是当您使用apply_async 时,子进程可能无法像主进程的while len(pending) > 0 循环一样快地更改pending 列表,然后主进程将调用更多次queue.get(block=True)队列的大小,因此主进程阻塞在queue.get。

【讨论】:

  • 嗯好吧,让我尝试解决这个问题并用一个可运行的示例更新我的答案!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 2016-12-03
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多