title: Basic multiprocessing with while loop
body: <p>I am brand new to the <code>multiprocessing</code> package in python and my confusion will probably be easy for someone who knows more to clear up. I've been reading about concurrency and have searched for other questions like this and have found nothing. (FYI I do <em>NOT</em> want to use <code>multithreading</code> because the GIL will slow down my application a lot.)</p>
  
  <p>I am thinking in the framework of events. I want to have multiple processes running, waiting for an event to happen. If the event happens, it gets assigned to a particular process, which operates and then returns to its idle state. There may be a better way to do this, but my reasoning is that I should spawn all the processes once and keep them open indefinitely, rather than creating then closing a process every time an event happens. Speed is an issue for me and my events can occur many thousands of times per second. </p>
  
  <p>I came up with the following toy example, which is meant to send even numbers to one process, and odd numbers to another. Both processes are the same, they just append the number to a list. </p>
  
  <pre><code>from multiprocessing import Process, Queue, Pipe
  
  slist=['even','odd']
  
  Q={}
  Q['even'] = Queue()
  Q['odd'] = Queue()
  
  ev,od = [],[]
  
  Q['even'].put(ev)
  Q['odd'].put(od)
  
  P={}
  P['even'] = Pipe()
  P['odd'] = Pipe()
  
  
  
  def add_num(s):
      """ The worker function, invoked in a process. The results are placed in
          a list that's pushed to a queue."""
  #    while True :
      if not P[s][1].recv():
          print s,'- do nothing'
  
      else:            
          d = Q[s].get()
          print d
          d.append(P[s][1].recv())
          Q[s].put(d)
          print Q[s].get()
          P[s][0].send(False)
          print 'ya'
  
  
  
  
  def piper(s,n):
  
      P[s][0].send(n)    
      for k in [S for S in slist if S != s]:
          P[k][0].send(False) 
      add_num(s)
  
  
  procs = [ Process (
                     target=add_num,
                     args=(i,)
                     ) 
           for i in ['even','odd']]
  
  for s in slist: 
      P[s][0].send(False)
  
  for p in procs:
      p.start()  
      p.join()
  
  for i in range(10):
      print i
      if i%2==0:
          s = 'even'
      else:
          s = 'odd'
      piper(s,i)
  
  
  print 'results:', Q['odd'].get(),Q['even'].get()
  </code></pre>
  
  <p>This code produces the following: </p>
  
  <p><code>even - do nothing</code></p>
  
  <p>Any insight from the wise into this problem, where my code or reasoning falls short etc. would be greatly appreciated. </p>