Python Multiprocessing and Queue
<p>I am new to amazing world of python, was developing a test system consist of continuous sense and test run. i have three or more while loops of which one is producer and rest two are consumers. did not understand multiprocessing very well, here are a sample code, first loop will create a data and second loop will get the data, how to impliment this in a infinity while loop, i will stop loop in the main program but asking your kind help to understand data exchange between while loops</p>

<pre><code>from multiprocessing import Process,Queue
from time import sleep

q=Queue()
cnt=0

def send():
  global cnt
  while True:
        sleep(1)
        cnt=cnt+1
        q.put(cnt,False)
        print ("data Send:",cnt)

def rcv():
  while True:
      sleep(1)
      newdata=q.get(cnt, False)
      print ("data Received",newdata)

if __name__=='__main__':
      p1=Process(target=send)
      p2=Process(target=rcv)
      p1.start()
      p2.start()
      p1.join()
      p2.join()
</code></pre>

<p>I would suggest you to dive in to the <a href="https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes" rel="nofollow">documentation</a> of the multiprocessing-library you are using.</p>

<p>Basically, you have two options. Queue and Pipe. Now you are using Queue, </p>

<pre><code>q.put(cnt,False)
...
newdata=q.get(cnt, False)
</code></pre>

<p>this will crash because you will try to get data from and empty Queue at some point, so you will need to check the queue status before reading from it.</p>

<pre><code>while not q.empty() and not q.task_done():
    newdata = q.get(cnt)
</code></pre>

<p>Other than that, if you want to have multiple receivers, you need to think about some kind of mutexes (see <a href="https://docs.python.org/2/library/multiprocessing.html#synchronization-between-processes" rel="nofollow">multitprocessing.Lock</a>), or multiprocessing.Pipe, since if one reader-process is just getting a value from the queue, and another one is checking the status of the queue, it will fail because the queue will actually be empty when the second one tries to read from it.</p>

<p>However, for this minimal example, using a mutex (<em>mutual exclusive lock</em>, prevents multiple processes accessing the same memory at the same time), will most likely negate the advantage gained when using multiple cores. However, if the different processes actually do some heavy calculation with the values before/after accessing the queue, the benefit gained will be greater than the loss from using a lock.</p>

python