Home > Backend Development > Python Tutorial > How to Share a Queue Between Multiple Asynchronous Worker Processes in Multiprocessing?

How to Share a Queue Between Multiple Asynchronous Worker Processes in Multiprocessing?

Barbara Streisand
Release: 2024-10-19 18:42:02
Original
1012 people have browsed it

How to Share a Queue Between Multiple Asynchronous Worker Processes in Multiprocessing?

Sharing Results Across Multiple Processes

In multiprocessing, sharing resources, such as queues, between different processes can be a challenge. This article explores how to share a result queue among multiple asynchronous worker processes using the apply_async function.

The provided sample code, using multiprocessing.Process, fails due to the limitation of sharing queue objects across processes through inheritance. To overcome this restriction, the multiprocessing.Manager class offers a solution.

Using multiprocessing.Manager

multiprocessing.Manager provides a way to create and manage objects that can be shared across processes. The following code demonstrates how to use Manager to share a queue among worker processes:

<code class="python">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))</code>
Copy after login

By using m.Queue() instead of multiprocessing.Queue(), the queue is created and managed by the Manager object. This allows the worker processes to access and modify the queue without facing the "Queue objects should only be shared between processes through inheritance" error.

The above is the detailed content of How to Share a Queue Between Multiple Asynchronous Worker Processes in Multiprocessing?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template