×

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

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

How to Write a Configuration file in Python

This article will discuss How to write a configuration file in python, why we need config files in Python, the format of the configuration file, file extensions, and how to...

11 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Crash Course on Python by Google

There is a new, free Python programming course offered by Google on Coursera. No programming experience is necessary. There are numerous Python courses available, but when you learn that Google is...

5 minutes read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Python divmod() function

Python divmod() function The divmod() function in Python returns a tuple containing the quotient  and the remainder when parameter ‘a’ (divident) is divided by parameter ‘b’ (divisor). Syntax: divmod(a, b) Parameter a: This parameter represents a number you...

1 minute read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Collections in python

In this tutorial, we will see what is collection in python.  Further, we will see in-depth the different kinds of collections in python. Collections Collections in python are the built-in module used...

9 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 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.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

Base Case in Recursive function python

In this article, we will learn about Python's base case in a recursive function. Before learning this, let’s first understand what recursion is in Python and the use of recursion in...

6 minutes read.