I have a program which uses both, threads and processes. To share a data between them, I currently use a multiprocessing.Queue
. Is this queue implementation thread safe?
The first function will take the multiprocessing queue as an input argument. While execution, it will add positive numbers from 1 to 1000 to the Python multiprocessing queue. The second function will also take the multiprocessing queue as an input argument. However, it will add negative numbers from -1000 to -1 to the multiprocessing queue.
The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module. The module implements three types of queue, which differ only in the order in which the entries are retrieved.
If a process is killed using Process.terminate () or os.kill () while it is trying to use a Queue, then the data in the queue is likely to become corrupted. This may cause any other process to get an exception when it tries to use the queue later on.
We can use the put () method to insert an element into the multiprocessing queue. When invoked on a multiprocessing queue, the method takes an element as its input argument and adds the element to the queue, and after execution, it returns None.
Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes:
Queues are thread and process safe.
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