title: Python - multi-process code returns results to a queue successfully, but the code stops
body: <p>Wonder if you can help?  I'm new to Python, and am trying to put together a simple piece of code that calculates prime numbers in parallel (across multiple CPU cores)...  I know it's lame, but it seemed like a reasonable piece of code to learn about Python threads etc.</p>
  
  <p>I have successfully managed to run the code using Threads, but am now investigating Process(es).</p>
  
  <p>The code below runs fine up to the point where i'm gathering the results.  I've tried to debug using eclipse &amp; pdev debugger and have found the While loop that i'm using to remove items from outputQueue seems to stick after 3 iterations, so the code never actually reaches the stage where it outputs the results.</p>
  
  <p>Any ideas / advice / help would be greatly appreciated.</p>
  
  <p>Many thanks</p>
  
  <p>Craig
  ------ code</p>
  
  <pre><code>from multiprocessing import Process, Queue
  
  # return true if number is prime, else false
  def calcPrime(number):    
      divisor=2
      if number%2==0: 
          return False
  
      numberIsPrime=True
      while divisor*divisor &lt;= number:   
          if number % divisor == 0:
              numberIsPrime = False
              break
          divisor = divisor + 1
      return numberIsPrime
  
  # generate list of primes
  def generatePrimes(minimum, maximum):
      return [num for num in range(minimum,maximum) if calcPrime(num)==True]
  
  def workerThread(output, mn, mx):
      primelist=generatePrimes(mn,mx)
      output.put(primelist)
  
  def main():
      outputQueue=Queue()
  
      t=[]
      t.append(Process(target=workerThread, args=(outputQueue, 1,25000)))
      t.append(Process(target=workerThread, args=(outputQueue, 25001, 50000)))
      t.append(Process(target=workerThread, args=(outputQueue, 50001, 75000)))
      t.append(Process(target=workerThread, args=(outputQueue, 75001, 100000)))
  
      #start all threads
      for idx in range(len(t)):
          t[idx].daemon=True
          t[idx].start()
  
      #wait for all process threads to complete
      for p in t:
          p.join()
      print("Processes finished")
  
      # gather all results
      l=[]
      while True:
          try:  
              l+=outputQueue.get()  # Code seems to stick here after about 3-4 iterations
          except:
              break
  
      #print out our lovely primes
      for idx in range(len(l)):
          print (str(l[idx]))    
  
  # standard code        
  if __name__ == '__main__':
      main()
  </code></pre>