Python multiprocessing pipe will not recv() properly

谁说胖子不能爱 提交于 2019-12-10 22:42:18

问题


This is for Python 3.2.2. I'm only just learning how Python and multiprocessing work and this simple example has been tripping me:

from multiprocessing import Pipe, Process

def f(r):
    print(r.recv())

if __name__ == '__main__':
    q, r = Pipe()
    p = Process(target=f, args=(r,))
    p.start()
    q.send([42, None, 'hello'])
    p.join()

The main thread creates a new Process, p, and sends r, a bidirectional connection object, to function f(). When process p is started, I expect r.recv() to block (which, as far as I understand, means that this process will wait indefinitely until something comes through the pipe) until the main process sends some objects through with q.send.

Then p.join() should make the main process wait until p has run its course.

But nothing whatsoever happens. If I add a print statement to f(), nothing happens there, either, as if f() never even runs and p.start() is nonfunctional.

Can you explain why this won't work and what might be fixed?


回答1:


I know it's been a while, but for others with this problem, you have the ends of your pipe reversed. You're trying to use the receiving end to send, and trying to receive with the sending end. I find that adding duplex=True to the Pipe constructor makes it much easier to deal with the different ends.

Source: https://docs.python.org/2/library/multiprocessing.html#pipes-and-queues




回答2:


From experience I've found that I can't print from processes that I've started. You could try reversing your program:

from multiprocessing import Pipe, Process

def f(r):
    r.send([42, None, 'hello'])

if __name__ == '__main__':
    q, r = Pipe()
    p = Process(target=f, args=(r,))
    p.start()
    print(q.recv())
    p.join()


来源:https://stackoverflow.com/questions/10442870/python-multiprocessing-pipe-will-not-recv-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!