Python RLock Class | acquire() Method with Example

Python RLock.acquire() Method: Here, we are going to learn about the acquire() method of RLock Class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 18, 2020

Python RLock.acquire() Method

acquire() is an inbuilt method of the RLock class of the threading module in Python.

RLock class objects follow reentrancy. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; and the thread must release it once for each time it has acquired it. The acquire method is used to acquire a lock, either blocking or non-blocking.

Module:

    from threading import RLock

Syntax:

    acquire( blocking=True, timeout=-1)

Parameter(s):

  • blocking: It is an optional parameter, which works as a blocking flag. Its default value is True. If,
    • The value is True: If the thread already owns the lock, increment the recursion level by one and return; and if another thread owns the lock, block until the lock is released by that thread. Once the thread gets released, acquire the lock and increment the recursion level by one and return.
    • The value is False: Don't block.
  • timeout: It is an optional parameter, which specifies the number of seconds for which the calling thread will be blocked if some other method is acquiring the lock currently. Its default value is -1 which indicates that the thread will be blocked for an indefinite time till it acquires the lock.

Return value:

The return type of this method is <class 'bool'>. The method is used to acquire a lock while implementing multithreading and returns True if lock was successfully acquired else returns False.

Example:

# program to illustrate the use of 
# acquire() method in RLock class
  
# importing the module 
import threading 
  
# initializing the shared resource 
x = 0
  
# creating an RLock object  
rlock = threading.RLock() 
  
# Creating first thread
rlock.acquire() 
x = x + 1
  
# the below thread is trying to access  
# the shared resource  
rlock.acquire()  
x = x + 2
rlock.release()

rlock.release() 
# Rlock released by both the threads
  
# displaying the value of shared resource 
print("Displaying the final value of the shared resource x:", x)

Output

Displaying the final value of the shared resource x: 3






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.