Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think as a part of making python multi-platform, simple things like SIGCHLD must be done yourself. Agreed, this is a little more work when all you want to do is know when the child is done, but it really isn't THAT painful. Consider the following that uses a child process to do the work, two multiprocessing.Event instances, and a thread to check if the child process is done:</p> <pre><code>import threading from multiprocessing import Process, Event from time import sleep def childsPlay(event): print "Child started" for i in range(3): print "Child is playing..." sleep(1) print "Child done" event.set() def checkChild(event, killEvent): event.wait() print "Child checked, and is done playing" if raw_input("Do again? y/n:") == "y": event.clear() t = threading.Thread(target=checkChild, args=(event, killEvent)) t.start() p = Process(target=childsPlay, args=(event,)) p.start() else: cleanChild() killEvent.set() def cleanChild(): print "Cleaning up the child..." if __name__ == '__main__': event = Event() killEvent = Event() # process to do work p = Process(target=childsPlay, args=(event,)) p.start() # thread to check on child process t = threading.Thread(target=checkChild, args=(event, killEvent)) t.start() try: while not killEvent.is_set(): print "GUI running..." sleep(1) except KeyboardInterrupt: print "Quitting..." exit(0) finally: print "Main done" </code></pre> <h2>EDIT</h2> <p>Joining to all processes and threads created is a good practice because it will help indicate when zombie (never-finishing) processes/threads are being created. I've altered the above code making a ChildChecker class that inherits from threading.Thread. It's sole purpose is to start a job in a separate process, wait for that process to finish, and then notify the GUI when everything is complete. Joining on the ChildChecker will also join the process it is "checking". Now, if the process doesn't join after 5 seconds, the thread will force terminate the process. Enter "y" creates starts a child process running "endlessChildsPlay" that must demonstrate force termination. </p> <pre><code>import threading from multiprocessing import Process, Event from time import sleep def childsPlay(event): print "Child started" for i in range(3): print "Child is playing..." sleep(1) print "Child done" event.set() def endlessChildsPlay(event): print "Endless child started" while True: print "Endless child is playing..." sleep(1) event.set() print "Endless child done" class ChildChecker(threading.Thread): def __init__(self, killEvent): super(ChildChecker, self).__init__() self.killEvent = killEvent self.event = Event() self.process = Process(target=childsPlay, args=(self.event,)) def run(self): self.process.start() while not self.killEvent.is_set(): self.event.wait() print "Child checked, and is done playing" if raw_input("Do again? y/n:") == "y": self.event.clear() self.process = Process(target=endlessChildsPlay, args=(self.event,)) self.process.start() else: self.cleanChild() self.killEvent.set() def join(self): print "Joining child process" # Timeout on 5 seconds self.process.join(5) if self.process.is_alive(): print "Child did not join! Killing.." self.process.terminate() print "Joining ChildChecker thread" super(ChildChecker, self).join() def cleanChild(self): print "Cleaning up the child..." if __name__ == '__main__': killEvent = Event() # thread to check on child process t = ChildChecker(killEvent) t.start() try: while not killEvent.is_set(): print "GUI running..." sleep(1) except KeyboardInterrupt: print "Quitting..." exit(0) finally: t.join() print "Main done" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload