How to share a result queue among several processes with Python?

Sometimes, we want to share a result queue among several processes with Python.

In this article, we’ll look at how to share a result queue among several processes with Python.

How to share a result queue among several processes with Python?

To share a result queue among several processes with Python, we use the Queue class.

For instance, we write

import multiprocessing
def worker(name, que):
    que.put("%d is done" % name)

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=3)
    m = multiprocessing.Manager()
    q = m.Queue()
    workers = pool.apply_async(worker, (33, q))

to create a queue from the Manager object with m.Queue.

Then we call pool.apply_async with the worker function and (33, q) to call worker with 33 and queue q as the arguments.

In worker, we call que.put to return a result after worker is done running.

Conclusion

To share a result queue among several processes with Python, we use the Queue class.