SoFunction
Updated on 2025-03-02

Sharing tips on using locks under Python threads

When using lock primitives such as Lock, RLock, Semphore, you must be careful. Incorrect use of locks can easily lead to deadlocks or competition with each other. The code that depends on the lock should ensure that the lock can be released normally when an exception occurs.

Typical codes are as follows:

try:
  ()
  #Key Part  ...
finally:
  ()

In addition, all kinds of locks also support context management protocols (which are more concise to write):

The with statement automatically acquires the lock and automatically releases the lock when the control flow leaves the context.

with lock:
  #Key Part  ...

In addition, when writing code, you should generally avoid obtaining multiple locks at the same time. For example, try to avoid it as much as possible:

This notification is uniform to cause a mysterious deadlock for the application, and although this can be avoided with centralized policies (such as hierarchical locking), it is best to avoid such nested locks when writing code.

with lock_A:
  #Key Part  ...
  with lock_B:
    The key part of #B    ...

Although it is possible to write very traditional multithreaded programs in Python using a combination of various locks and synchronization primitives, there is a first-hand programming method that is better than all other programming methods: that is, to organize multithreaded programs into a collection of multiple independent tasks, which communicate through a message queue, such as the queue module to be discussed below.