In this verbose example, we'll explore exactly how we can implement a communication mechanism between distinct child processes using the queue synchronization primitive.
We define the myTask function, which takes in our shared queue as a parameter, and simply pops a value from the queue. Within our main function, we then define both our manager object and our shared queue using this:
m = multiprocessing.Manager()
sharedQueue = m.Queue()
We then define three separate processes that take in the myTask function as their target for execution, and the sharedQueue as their one and only argument. We then start and join each of these processes in turn:
from multiprocessing import Pool
import multiprocessing
import queue
import time
def myTask(queue):
value = queue.get()
print("Process {} Popped {} from the shared Queue".format(multiprocessing.current_process().pid, value))
queue.task_done()
def main():
m = multiprocessing.Manager()
sharedQueue = m.Queue()
sharedQueue.put(2)
sharedQueue.put(3)
sharedQueue.put(4)
process1 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
process1.start()
process1.join()
process2 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
process2.start()
process2.join()
process3 = multiprocessing.Process(target=myTask, args=(sharedQueue,))
process3.start()
process3.join()
if __name__ == '__main__':
main()