In python, it supplies a lot of ways to communicate between processes in module multiprocessing, Pipe
, Queue
, Value
, Array
and Manager
. Which of them are better choices?
Every object has two methods – send() and recv(), to communicate between processes.
Passing Messages to Processes A simple way to communicate between process with multiprocessing is to use a Queue to pass messages back and forth. Any pickle-able object can pass through a Queue. This short example only passes a single message to a single worker, then the main process waits for the worker to finish.
The possible start methods are 'fork', 'spawn' and 'forkserver'. On Windows only 'spawn' is available. On Unix 'fork' and 'spawn' are always supported, with 'fork' being the default.
If we are using the context manager to create the process pool so that it is automatically shutdown, then you can configure the number of processes in the same manner. The number of workers must be less than or equal to 61 if Windows is your operating system.
Use Pipe and Queues if you want to implement message-passing. Use Value and Array if you want to implement shared memeory. Use Managers if you want to expose an object-oriented interface to multiple processes.
Pipe
is good for 1-to-1 communication or for byte-level protocols:
Queue
is similar to a unidirectional Pipe, but may work in many-to-many scenarios:
Queue is implemented using a pipe and some locks/semaphores.
Value
and Array
:
ctypes
)Managers:
Value
and Array
is a lightweight approach to shared memory. In my experience, the overhead of using a SyncManager
and AutoProxy
can be huge. If you can solve your problem using a Value
or an Array
, use them. SyncManager
may be useful to expose an object-oriented interface to multiple processes, unless it is not called too frequently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With