python中locked_Python锁类| 带示例的locked()方法

python中locked

Python Lock.locked()方法 (Python Lock.locked() Method)

locked() is an inbuilt method of the Lock class of the threading module in Python.

Locked()是Python中线程模块的Lock类的内置方法。

This method returns True if the lock is acquired by a thread else returns False.

如果线程获取了锁,则此方法返回True ,否则返回False

Module:

模块:

    from threading import Lock

Syntax:

句法:

    locked()

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.

此方法的返回类型为<class'bool'> 。 返回True是当前获取的锁,否则返回False

Example:

例:

# 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


翻译自: https://www.includehelp.com/python/lock-locked-method-with-example.aspx

python中locked

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值