title: Python multiprocessing - pipe communication between processes
body: <p>I am making a project which collects data from clients sensors, processes the gathered data and sends it back to clients. There can be multiple clients asking to receive some data from our server at the same time, so I had to implement multiprocessing. I can't use Threads because there are certain variables that must be client independent. If I did, my code would probably get very complicated to read and upgrade, and I don't want that. So I decided to use Processes, but now there is some data that needs to be sheared between parent and child Processes. After some research, I found that Pipe communication would satisfy my requirements.</p>
  
  <p>The following code successfully sends data from parent to child Process, child updates the data and sends it back to the parent. But it is working only because of the sleep() function that stops the parent from using the pipe at the same time as the child.</p>
  
  <p>How can it be changed so it does the same, but without the sleep() function for which I believe it will most probably cause problems in the future?  </p>
  
  <pre><code>from multiprocessing import Process, Pipe
  import time
  
  def update_data(pipe):
      p_out, p_in = pipe
      L = []
      while True:
          message = p_out.recv()
          if message=='FINISHED':
              break       
          L.append(message)      
  
      L.append(['new data'])       #updating received data
      writer(L, p_in)              #sending received data to parent Process
      p_in.close()
  
  def writer(i, p_in):
      p_in.send(i)
      p_in.send('FINISHED')
  
  L = ['0' for i in range(10)]     #current data
  if __name__=='__main__':
      p_out, p_in = Pipe()
      update_data_process = Process(target=update_data, args=((p_out, p_in),))
      update_data_process.start()    
      writer(L, p_in)              #sending current data to child Process
      time.sleep(3)                #needs to be changed
      while True:
          message = p_out.recv()
          if message != 'FINISHED':
              L = message
          else:
              break
      print(L)
      p_in.close()
      update_data_process.join()
  </code></pre>