Search⌘ K

Locks & Reentrant Lock

Explore how to implement Locks and Reentrant Locks in Python's multiprocessing module to synchronize processes. Understand fixing common concurrency bugs with locks and managing reentrant locks that a process can acquire multiple times without blocking, enabling safer parallel execution.

We'll cover the following...

Locks & Reentrant Lock

Similar to the threading module, synchronization primitives exist for the multiprocessing module to coordinate among multiple processes. The primitives between the two modules have a lot of commonalities.

Because of the similarities, we just reproduce the examples from the threading section using the primitives from the multiprocessing module.

Lock

Lock is a non-recursive object and shares the same DNA as the threading.Lock class. In the section on using Queues and Pipes we introduced a snippet with a bug, that could potentially hang depending on the order in which the two processes consumed objects placed on the queue. We can ...