1

In the following script, I get the "stop message received" output but the process never ends. Why is that? Is there another way to end a process besides terminate or os.kill that is along these lines?

from multiprocessing import Process
from time import sleep

class Test(Process):
    def __init__(self):
        Process.__init__(self)
        self.stop = False

    def run(self):
        while self.stop == False:
            print "running"
            sleep(1.0)

    def end(self):
        print "stop message received"
        self.stop = True

if __name__ == "__main__":
    test = Test()
    test.start()
    sleep(1.0)
    test.end()
    test.join()
3
  • Try print self.stop,"running" to see what the value of stop is. Also, try using the boolean tests for the while loop, such as "while not self.stop" and see what happens. Commented Apr 21, 2010 at 19:14
  • I already tried that. What happens is self.stop is always False. However, if you print self.stop right after end() is called, it is True. Is it really True? Is something setting it back to False? Is Python lying to me? :) Commented Apr 21, 2010 at 19:40
  • My python here at work doesn't have the multiprocessing module so it's hard to definitively answer this, but all I can think of is, since the test object is in a different process from your main, they don't share states, so changing the state by calling test.end in your main process does not affect the state in the other process. This is, I think, the main difference between using multiprocessing and the threading module. You probably need to communicate by setting a pipe into the test process. Commented Apr 21, 2010 at 19:50

1 Answer 1

3

The start method has cloned the object into a separate process, where it executes run. The end method is nothing special, so it runs in the process that calls it -- the changes it performs to that object are not sent to the clone object.

So, use instead an appropriate means of interprocess communication, such as a multiprocessing.Event instance, e.g.:

from multiprocessing import Process, Event
from time import sleep

class Test(Process):
    def __init__(self):
        Process.__init__(self)
        self.stop = Event()

    def run(self):
        while not self.stop.is_set():
            print "running"
            sleep(1.0)

    def end(self):
        print "stop message received"
        self.stop.set()

if __name__ == "__main__":
    test = Test()
    test.start()
    sleep(1.0)
    test.end()
    test.join()

As you see, the required changes are minimal.

Sign up to request clarification or add additional context in comments.

Comments

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.