SoFunction
Updated on 2024-11-15

Explaining Lock and Relock in Python in Detail

A thread is an entity in a process that can be scheduled for execution. Moreover, it is the smallest processing unit in the operating system that can be executed. Simply put, a thread is a sequence of instructions in a program that can be executed independently of other code. For simplicity's sake, you can assume that a thread is just a subset of a process!

Locks

Locks are the simplest way to use synchronization in Python. Locks have two states: locking and releasing.

Locks are a class in the thread module with two main methods: acquire() and release() When the acquire() method is called, it locks the execution of the lock and blocks the execution of the lock until some other thread calls the release() method to set it to the unlocked state. Locks help us to access shared resources in the program efficiently to prevent data corruption, it follows mutual exclusion as only one thread can access a particular resource at a time.

Let's look at the following example to understand the use of locks.

import threading
 
# Create a lock object
lock = ()
 
# Initialize shared resources
abce = 0
 
def sumOne():
    global abce
 
    # Lock down shared resources
    ()
    abce = abce + 1
 
    # Release shared resources
    ()
 
def sumTwo():
    global abce
 
    # Lock down shared resources
    ()
    abce = abce + 2
 
    # Release shared resources
    ()
 
#call function
 
sumOne()
sumTwo()
print(abce)

In the above program, lock is a lock object, the global variable abce is a shared resource, sumOne () and sumTwo () function pretending to be two threads, in the sumOne () function in the shared resource abce first locked, then increased by 1, then abce is released. sumTwo () function performs similar operations. Two functions sumOne () and sumTwo () can not be accessed at the same time to the shared resource abce, only one at a time to access the shared resource.

RLocks

The default lock does not recognize which thread the lock is currently held by. If any thread is accessing a shared resource, other threads trying to access the shared resource will be blocked, even if the thread that locked the shared resource. In these cases, a reentrant lock (or RLock) is used to prevent unnecessary blocking when accessing the shared resource. If the shared resource is in an RLock, it is safe to call it again. An RLocked resource can be accessed repeatedly by different threads, even though it will still work when called by a different thread.

Let's look at the following example to understand the use of RLocks.

import threading
 
# Create a lock object
lock = ()
 
# Initialize shared resources
abce = 0
 
# This thread accesses shared resources
()
abce = abce + 1
 
#This thread will be blocked from accessing shared resources
()
abce = abce + 2
()
 
print(abce)

In the above program, two threads are trying to access the shared resource abce at the same time, here when one thread is currently accessing the shared resource abce, the other thread will be prevented from accessing it. When two or more threads try to access the same resource, they effectively block each other from accessing that resource, this is known as deadlock, hence the above program does not generate any output.

However, the above problem can be solved in the program by using RLock.

import threading
 
# Create an rlock object
lock = ()
 
# Initialize shared resources
abce = 0
 
# This thread accesses shared resources
()
abce = abce + 1
 
# This thread tries to access a shared resource
()
abce = abce + 2
()
 
print(abce)

Here, no thread in the program is prevented from accessing the shared resource abce. For each acquire() of the RLock object lock, we need to call release() once.

From the numerous programs and explanations mentioned above, there are many differences between a Lock object and an RLock object in Python:.

locks rlocks
The lock object can no longer be acquired by another thread unless the holding thread releases the rlock objects can be fetched multiple times by other threads
Lock objects can be released by any thread An rlock object can only be released by the thread that holds it.
The lock object cannot be owned by any thread. rlock objects can be owned by multiple threads
Locking on an object is fast Adding an rlock to an object is slower than adding a lock

Above is a detailed explanation of Lock and Rlock in Python in detail, more information about Lock and Rlock in Python please pay attention to my other related articles!