Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python multiprocessing.Queue thread safe?

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?

like image 961
tomasbedrich Avatar asked Jan 22 '16 00:01

tomasbedrich


People also ask

How to use the multiprocessing queue in Python?

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.

What is queue class in Python?

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.

What happens when a process is killed while using a queue?

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.

How to insert an element into a multiprocessing queue in Java?

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.


1 Answers

Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes:

Queues are thread and process safe.

like image 62
tomasbedrich Avatar answered Sep 19 '22 23:09

tomasbedrich