×

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation in python.

Multithreading in Python

Threads can be defined as lightweight processes that run in a certain order in order to perform a certain task. Nowadays, when computers have multiple processors it has enabled the user to run multiple threads at the same time to perform several tasks parallelly. This process of executing multiple threads at the same time is what we call multithreading.

Threading in Python

Python has a threading module, using this module we can easily implement multithreading in our program. This module enables the user not only to create threads but we can also to control and manage those threads in python.

Implementation of Threading

To implement threading in the program, the Thread module provides certain classes whose object performs specific functioning while executing these threads.

Some of the important objects in the threading module are:

ObjectsDescription
ThreadIt represents the execution of a single thread.
LockIt is the primitive lock with only two states that is Lock and Unlock. Once unlocked it cannot be reversed.
RLockIt allows recursive locking which means we can acquire a released lock again.
ConditionIt is the phase where a lock awaits while a certain lock awaits for the other lock to perform its task.
EventIt involves several locks waiting for a particular event to happen.
SemaphoreAn internal counter for the shared resources
TimerThis represents the execution of the thread, along with a time constraint for which the thread requires to wait before execution.
BarrierThis object stops the threads at a point until a required number of threads reach that point.

Race Condition

While performing multiple threading it might be possible that multiple threads try to access and modify the same resource. If all the locks are allowed to modify the resource at the same time it would produce erroneous results. This condition where multiple threads modify the shared resource concurrently is called the Race Condition. It is necessary to prevent this to prevent the data from getting corrupted.

To avoid this situation, we need to synchronize the access and modification of the resources by various threads. This is done by using locks in python.

Primitive Lock Object

Any lock in python can either be:

  1. Locked
  2. Unlocked

For locking the lock, we use acquire() method. Once the thread is in this stage, it cannot be acquired by any other thread and the resource is limited to the particular thread until the lock is released.

To release a lock, we need to use the release() method. When we use this method on any locked lock it automatically changes its state into unlocked. If we use this method on an already unlocked lock, then it will throw an error.

acquire() method: This method is used to acquire the lock on the thread, using this method we can lock the thread until we wish to free the thread or we can pass two optional parameters specifying when to unlock the lock.

acquire(blocking=True, Timeout=-1) method: Two arguments can be passed in this method.

  1. blocking: if this argument is passed as False, it will not block the thread even when the lock is being acquired by any other thread, it will only be blocked if this flag is passed as True. Once set True it will acquire the lock and return True as the value otherwise it would not acquire the lock and return False as the value.
  2. Timeout: this argument holds the number of seconds till which you want the thread to be locked. The default value to this parameter is -1, this specifies that the lock is to be acquired for a definite time. We need to pass positive floating-point values to this parameter.

release() method: This method is used to free the lock, if the lock is locked, and the thread has been executed we release the lock with the help of using this method; so that it can be accessed by other threads in the program. It resets the lock to unlock. If this method is invoked on an already unlocked lock, this will throw a Runtime error.

RLock Objects

RLock stands for Re-entrant Lock. The problem that arise by using primitive lock was that we were not able to re-lock the thread. That means we cannot use the acquire() method on a lock twice. To solve this problem, we use RLock. The primitive lock only performs the locking without recognising that which is the thread holding the lock. It prevents unnecessary blocking from accessing the shared resources. If we have a resource that is locked using re-entrant lock, the resource can be called and accessed again without getting blocked. But unlike the primitive lock, this lock can only be released by the thread that is currently using this lock. It makes the accessing of shared resources easier and safer.

Now, let’s implement what we have learned:

import threading
import time
from random import randint


class SCnt(object):

    def __init__(self, value=0):
        self.lock = threading.Lock()
        self.counter = value

    def increment(self):
        print("Wait while the lock is being acquired")
        self.lock.acquire()
        try:
            print('lock is acquired, counter value: ', self.counter)
            self.counter = self.counter + 1
        finally:
            print('lock is freed, counter value: ', self.counter)
            self.lock.release()


def task(c):
    # Choose a random number
    random_no = randint(1, 5)
    # performing  increment on the random numbers
    for i in range(random_no):
        c.increment()
    print('Done')


if __name__ == '__main__':
    sCounter = SCnt()

    task1 = threading.Thread(target=task, args=(sCounter,))
    task1.start()

    task2 = threading.Thread(target=task, args=(sCounter,))
    task2.start()

    print('Wait for the worker threads to get loaded')
    task1.join()
    task2.join()

    print('Counter:', sCounter.counter)

In this Program, we have created a class SCnt that is being shared among various threads. The task method we have called the increment() function will access the same counter and will increment the value of the counter. The value of the counter is inconsistent due to the concurrent modifications that are being performed on it.


Related Topics

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python while loop

Loops are essential in Python or any other programming language, as they help to execute a block of code repetitively. Sometimes, situations arise where you would need to use a piece of code...

2 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

How to Install Python In Windows

How To Install Python In Windows? Python is a language that every aspirant developer looks forward to. One thing we must keep in our mind is how to begin our hands-on...

3 minutes read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.