Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your options are a bit limited. If doing <code>self.daemon = True</code> in the constructor for the <code>Worker</code> class does not solve your problem and trying to catch signals in the Parent (ie, <code>SIGTERM, SIGINT</code>) doesn't work, you may have to try the opposite solution - instead of having the parent kill the children, you can have the children commit suicide when the parent dies.</p> <p>The first step is to give the constructor to <code>Worker</code> the <code>PID</code> of the parent process (you can do this with <code>os.getpid()</code>). Then, instead of just doing <code>self.queue.get()</code> in the worker loop, do something like this:</p> <pre><code>waiting = True while waiting: # see if Parent is at home if os.getppid() != self.parentPID: # woe is me! My Parent has died! sys.exit() # or whatever you want to do to quit the Worker process try: # I picked the timeout randomly; use what works data = self.queue.get(block=False, timeout=0.1) waiting = False except queue.Queue.Empty: continue # try again # now do stuff with data </code></pre> <p>The solution above checks to see if the parent PID is different than what it originally was (that is, if the child process was adopted by <code>init</code> or <code>lauchd</code> because the parent died) - see <a href="http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg55718.html" rel="nofollow noreferrer">reference</a>. However, if that doesn't work for some reason you can replace it with the following function (adapted from <a href="https://stackoverflow.com/questions/568271/check-if-pid-is-not-in-use-in-python">here</a>):</p> <pre><code>def parentIsAlive(self): try: # try to call Parent os.kill(self.parentPID, 0) except OSError: # *beeep* oh no! The phone's disconnected! return False else: # *ring* Hi mom! return True </code></pre> <p>Now, when the Parent dies (for whatever reason), the child Workers will spontaneously drop like flies - just as you wanted, you daemon! <code>:-D</code></p>
    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. 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