Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing pipe will not recv() properly

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?

like image 230
Vijchti Avatar asked Feb 05 '26 14:02

Vijchti


2 Answers

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

like image 81
Magitrek Avatar answered Feb 08 '26 03:02

Magitrek


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()
like image 32
fdvfcges Avatar answered Feb 08 '26 03:02

fdvfcges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!