Interprocess communication
While locks are one of the most common synchronization primitives that are used for communication among threads, pipes and queues are the main way of communicating between different processes. Specifically, they provide message-passing options to facilitate communication between processes—pipes for connections between two processes and queues for multiple producers and consumers.
In this section, we will be exploring the usage of queues, specifically the Queue
class from the multiprocessing
module. The implementation of the Queue
class is, in fact, both thread-and process-safe, and we have already seen the use of queues in Chapter 10, Working with Threads in Python. All pickleable objects in Python can be passed through a Queue
object; in this section, we will be using queues to pass messages back and forth between processes.
Using a message queue for interprocess communication is preferred over having shared resources since, if certain processes mishandle and corrupt...