0

I am trying to create some multiprocessing code for my project. I have created a snippet of the things that I want to do. However its not working as per my expectations. Can you please let me know what is wrong with this.

from multiprocessing import Process, Pipe
import time

class A:
      def __init__(self,rpipe,spipe):
          print "In the function fun()"

      def run(self):
           print"in run method"
           time.sleep(5)
           message = rpipe.recv()
           message = str(message).swapcase()
           spipe.send(message)

 workers = []
 my_pipe_1 = Pipe(False)
 my_pipe_2 = Pipe(False)
 proc_handle = Process(target = A, args=(my_pipe_1[0], my_pipe_2[1],))
 workers.append(proc_handle)
 proc_handle.run()
 my_pipe_1[1].send("hello")
 message = my_pipe_2[0].recv()
 print message
 print "Back in the main function now"

The trace back displayed when i press ctrl-c:

 ^CTraceback (most recent call last):
 File "sim.py", line 22, in <module>
 message = my_pipe_2[0].recv()
 KeyboardInterrupt

When I run this above code, the main process does not continue after calling "proc_handle.run". Why is this?

7
  • Sorry about the formatting, I could not format it properly in this editor. Commented Jun 13, 2014 at 17:18
  • Do you get error messages of any kind? Commented Jun 13, 2014 at 17:20
  • what is "ph.run"? I don't see that anywhere in the code... Commented Jun 13, 2014 at 17:20
  • Thanks for showing me multiprocessing.Pipe -- I had never seen that method of communicating between processes before. Commented Jun 13, 2014 at 17:22
  • ph.run is proc_handle.run() I presume. Commented Jun 13, 2014 at 17:23

2 Answers 2

1

You've misunderstood how to use Process. You're creating a Process object, and passing it a class as target, but target is meant to be passed a callable (usually a function) that Process.run then executes. So in your case it's just instantiating A inside Process.run, and that's it.

You should instead make your A class a Process subclass, and just instantiate it directly:

#!/usr/bin/python

from multiprocessing import Process, Pipe
import time

class A(Process):
      def __init__(self,rpipe,spipe):
          print "In the function fun()"
          super(A, self).__init__()
          self.rpipe = rpipe
          self.spipe = spipe

      def run(self):
           print"in run method"
           time.sleep(5)
           message = self.rpipe.recv()
           message = str(message).swapcase()
           self.spipe.send(message)

if __name__ == "__main__":    
    workers = []
    my_pipe_1 = Pipe(False)
    my_pipe_2 = Pipe(False)
    proc_handle = A(my_pipe_1[0], my_pipe_2[1])
    workers.append(proc_handle)
    proc_handle.start()
    my_pipe_1[1].send("hello")
    message = my_pipe_2[0].recv()
    print message
    print "Back in the main function now"

mgilson was right, though. You should call start(), not run(), to make A.run execute in a child process.

With these changes, the program works fine for me:

dan@dantop:~> ./mult.py 
In the function fun()
in run method
HELLO
Back in the main function now
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks dano, it helped me understand now. Is it the same way if I have to create a new thread too using threading.Thread? Will i have to just make class A inherit threading.Thread ?
@hemanths Yep, Process is designed to essentially be a drop-in replacement for Thread, so there's very few, if any, changes that need to be made when switching between the two.
@hemanths Though I'm not aware of a threading equivalent to multiprocessing.Pipe. You may want to use Queue.queue instead, should you switch to threads.
@dano: dummy replicates the Pipe interface for threads.
@JoelCornett Ah, excellent, thanks. Looks like internally it implements Pipe using two Queue.queue instances. So @hemanths, you would need to switch from multiprocessing import Pipe to from multiprocessing.dummy import Pipe in addition to switching from Process to Thread. You could even just switch to from multiprocessing.dummy import Process, Pipe, and leave everything the same. multiprocessing.dummy.Process is just a wrapper around threading.Thread.
0

Taking a stab at this one, I think it's because you're calling proc_handle.run() instead of proc_handle.start().

The former is the activity that the process is going to do -- the latter actually arranges for run to be called on a separate process. In other words, you're never forking the process, so there's no other process for my_pipe_1[1] to communicate with so it hangs.

1 Comment

Tried with proc_handle.start() too but no luck. Facing the same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.