如果我close()
管道的父节点,则poll()
会引发IOError
/ EOFError
,这是预期的行为。
当父母终止时,如何确保管道关闭,或者更糟糕的是,崩溃,以便孩子可以注意到另一端没有人?
以下代码将继续打印Sleeping...
,即使父母已经终止:
import sys
import time
from multiprocessing import Process, Pipe
def f(parent_conn, conn):
parent_conn.close() # Close unused end
for _ in range(10):
if conn.poll():
print 'Child:', conn.recv()
print 'Sleeping...'
time.sleep(0.3)
conn.close()
if __name__ == '__main__':
parent_conn, child_conn = Pipe()
p = Process(target=f, args=(parent_conn, child_conn))
p.start()
child_conn.close() # Close unused end
# parent_conn.close() # If uncommented, the child notices the pipe is closed
sys.exit(1)
答案 0 :(得分:1)
The multiprocessing.Pipe
object is a message oriented interface over a simple os.pipe
.
The multiprocessing.Connection.poll
method has several limitations. Not only it cannot detect closed pipes but it can also lead to deadlocks if the message sent from the other side of the pipe is too big to be read at once.
A workaround I usally employ which works only on Unix is using the select
function.
from select import select
from multiprocessing import Pipe
reader, writer = Pipe()
print('Waiting for data coming to the Pipe.')
if select([reader], [], [], None)[0]:
print('Data available on the Pipe.')
print(reader.recv())