使用multiprocessing.Pipe处理死进程

时间:2012-01-12 02:51:10

标签: python multiprocessing

最近在一个项目中,我遇到了一个崩溃的多处理过程。子进程应该进行计算,然后将其发送到Pipe中的父进程。如果孩子坠毁,父母会在从管道读取时冻结。是否有一种'正确'的方式来发送数据,以避免在孩子死亡时永远阻止父母?

这是一个再现我遇到的问题的例子:

import multiprocessing as mp

def f(pipe):
    a = 1/0
    pipe.send('hola')

parent, child = mp.Pipe()

proc = mp.Process(target=f, args=(child,))

proc.start()

print "Grabbing result"
print "Result: {0}".format(parent.recv())

proc.join()

1 个答案:

答案 0 :(得分:0)

父进程可以使用连接的poll(...)方法来确定是否有任何结果在合理的时间限制内发布:

import multiprocessing as mp

timelimit = 3

def f(pipe):
    a = 1/0
    pipe.send('hola')

parent, child = mp.Pipe()

proc = mp.Process(target=f, args=(child,))

proc.start()

print "Grabbing result"
if parent.poll(timelimit):
    print "Result: {0}".format(parent.recv())
else:
    print "No data available after {0} seconds...".format(timelimit)

proc.join()

当我运行此代码时,我得到以下结果:

Grabbing result
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "s.py", line 6, in f
    a = 1/0
ZeroDivisionError: integer division or modulo by zero
No data available after 3 seconds...