title: How to avoid defunct python processes in Linux?
body: <p>With python3 (3.4.3) on Ubuntu 14.04 I have created a <code>Forker</code> class which I use in two different processes to create child processes. Here is the <code>Forker</code> class:</p>
  
  <pre><code>class Forker(object):
      def __init__(self):     
          self.active_children = []            
  
      def reap_children(self):                              
          while self.active_children:                        
              pid,stat = os.waitpid(0, os.WNOHANG)       
              if not pid: break
              self.active_children.remove(pid)                
  
      def fork(self, function, *args, **kwargs):         
          self.reap_children()
  
          child_pid = os.fork()
          if child_pid == 0:
              function(*args, **kwargs)
              os._exit(0)
          else:                                               
              self.active_children.append(child_pid)
  
          return child_pid
  </code></pre>
  
  <p>In the two different Linux processes I derive from this class and then call e.g. something like:</p>
  
  <pre><code>self.fork(my_function, my_data)
  </code></pre>
  
  <p>However, I still get a couple of defunct python processes:</p>
  
  <pre><code>alexand+ 24777 24735  0 20:40 pts/29   00:00:00 [python3] &lt;defunct&gt;
  alexand+ 24838 24733  0 20:40 pts/29   00:00:00 [python3] &lt;defunct&gt;
  </code></pre>
  
  <p>Maybe there is something I can change to avoid these defunct processes?</p>