How to exchange objects between processes
The development of parallel applications has the need for the exchange of data between processes. The multiprocessing library has two communication channels with which it can manage the exchange of objects: queues and pipes.
Using queue to exchange objects
As explained before, it is possible for us to share data with the queue data structure.
A queue returns a process shared queue, is thread and process safe, and any serializable object (Python serializes an object using the pickable module) can be exchanged through it.
How to do it...
In the following example, we show you how to use a queue for a producer-consumer problem. The producer
class creates the item and queues and then, the consumer
class provides the facility to remove the inserted item:
import multiprocessing import random import time class producer(multiprocessing.Process): def __init__(self, queue): multiprocessing.Process.__init__...