Python Lock locked() Method with Example

Python Lock.locked() Method: In this tutorial, we will learn about the locked() method of Lock Class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 24, 2023

Python Lock.locked() Method

The Lock.locked() is an inbuilt method of the Lock class of the threading module, it returns True if the lock is acquired by a thread else returns False.

Module

The following module is required to use locked() method:

import threading

Class

The following class is required to use locked() method:

from threading import Lock

Syntax

The following is the syntax of locked() method:

locked()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'bool'>. It returns True is the lock is currently acquired else returns False.

Example of Lock.locked() Method in Python

# Python program to show
# the use of locked() method in Lock class

import threading
import random
                    
class shared(object):
  
    def __init__(self, x = 0):
        # Created a Lock object
        self.lock = threading.Lock()
        self.incr = x
        
        # Increment function for the thread
    def incrementcounter(self):
        print("Waiting for the lock to be unlocked")
        # Lock acquired by the current thread
        self.lock.acquire()
        print("Is the lock acquired here:", self.lock.locked())
        try:
            print('Lock acquired, current counter value: ', self.incr)
            self.incr = self.incr + 1
            print("Is the lock acquired here as well:", self.lock.locked())
        finally:
            print('Lock released, current counter value: ', self.incr)
            # Lock released by the given thread
            self.lock.release()
            print("Is the lock acquired here after release:", self.lock.locked())

def helper_thread(c):
    # Getting a random integer between 1 to 3
    r = random.randint(1,3)
    print("Random value selected:", r)
    for i in range(r):
      c.incrementcounter()
    print('Finished', str(threading.current_thread().getName()))
    print()

if __name__ == '__main__':
    obj = shared()
    thread1 = threading.Thread(target=helper_thread, args=(obj,))
    thread1.start()
    
    thread2 = threading.Thread(target=helper_thread, args=(obj,))
    thread2.start()

    thread1.join()
    thread2.join()
    
    print('Final counter value:', obj.incr)

Output

Waiting for the lock to be unlocked
Is the lock acquired here: True
Lock acquired, current counter value:  0
Is the lock acquired here as well: True
Lock released, current counter value:  1
Is the lock acquired here after release: False
Finished Thread-1

Random value selected: 3
Waiting for the lock to be unlocked
Is the lock acquired here: True
Lock acquired, current counter value:  1
Is the lock acquired here as well: True
Lock released, current counter value:  2
Is the lock acquired here after release: False
Waiting for the lock to be unlocked
Is the lock acquired here: True
Lock acquired, current counter value:  2
Is the lock acquired here as well: True
Lock released, current counter value:  3
Is the lock acquired here after release: False
Waiting for the lock to be unlocked
Is the lock acquired here: True
Lock acquired, current counter value:  3
Is the lock acquired here as well: True
Lock released, current counter value:  4
Is the lock acquired here after release: False
Finished Thread-2

Final counter value: 4

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.