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

python中locked_Python锁类| 带示例的locked()方法相关推荐

  1. python中用于释放类占用的资源的方法是()_mooc大学英语词汇期末答案

    把两个已有项目放到一起,就是一个新项目,这种项目来源属于(?? ) 答:整合 辩证法同形而上学的斗争 答:是从属于唯物主义同唯心主义的斗争,并同这种斗争交织在一起的 中国大学MOOC: 广义的计划是对 ...

  2. python中用于释放类占用的资源的方法是()_编写一个简易计算器,要求根据输入的数字和四则运算符号,计算运算结果并输出。_学小易找答案...

    [简答题]20191220 课前作业 新工作页4.1的3-5-3页的填空题,参考教材P135-P144 [简答题]AutoCAD改编视图,尽量不用虚线 1. 主视图采用局部剖,表达右上角小圆筒(及孔) ...

  3. python中用于释放类占用的资源的方法是()_2020超星尔雅龙华医院-五项答案新版...

    智慧职教: 某企业生产甲乙两种产品,销售额分别为100万元和400万元,边际贡献分别为50万元和200万元.则企业综合边际贡献率为( ). 答:0.5 以下成本中属于变动成本的有如(? )等. 答:进 ...

  4. python vector 初始化_一文带你走进Python中的数据类

    全文共2607字,预计学习时长14分钟 图源:unsplash 数据类适用于Python3.7或更高版本,它不仅可以用作数据容器,还可以编写样板代码,简化创建类的过程. 创建第一个数据类 创建一个数据 ...

  5. python中的定制类(转载)

    python中的定制类(转载)<?xml version="1.0" encoding="UTF-8"?> 看到类似__slots__这种形如__x ...

  6. Python中的元类是什么?

    元类是什么,我们将它们用于什么? #1楼 请注意,此答案适用于2008年编写的Python 2.x,元类在3.x中略有不同. 元类是使"类"工作的秘诀. 新样式对象的默认元类称为& ...

  7. Python中的元类及元类实现的单例模式

    https://www.cnblogs.com/tkqasn/p/6524879.html 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元 ...

  8. Python中的property类和@property装饰器

    Python中的property类和@property装饰器 在Python的类中,为了避免使用者直接在类的外部操作属性和方法,我们可以将属性和方法设置成私有属性和私有方法. 如果我们需要访问私有属性 ...

  9. python中不论类的名字是什么、构造方法的名字都是,在Python中,不论类的名字是什么,构造方法的名字都是________________。...

    在Python中,不论类的名字是什么,构造方法的名字都是________________. 更多相关问题 传导性失语最大特点为 六维作战空间是陆.海.空.天.电磁和_____ 三极管放大电路静态分析就 ...

最新文章

  1. day07 -文件的基本操作
  2. 应用程序的日志通过rsyslog推送到syslog服务器
  3. Java进阶学习路线
  4. 详解EBS接口开发之采购申请导入
  5. 读取txt原理_Mysql客户端任意文件读取学习
  6. php 自定义加密算法,php自定义加密函数、解密
  7. 汉字为什么能流传至今_女皇武则天自创18个汉字,有17个被废除了,只有这1个字流传至今...
  8. Swift实现OC中的单例模式
  9. 数据库实验3 数据库的单表查询
  10. Win10下Matlab r2018a 64位 中文破解版的安装以及破解方法
  11. 会计计算机,什么是好的计算机会计软件?
  12. 真正无广告的看书软件,免费可换源!-大萝卜博客网
  13. 深度学习——卷积神经网络原理解析
  14. 2020年的计算机专业就业形势,2020计算机专业就业分析
  15. 饥荒控制台输入没用_饥荒控制台怎么开启 饥荒控制台怎么用
  16. pl2303hxa串口线驱动_PL2303 USB转串口驱动64位(非认证线缆可用)_下载_热门驱动_驱动精灵...
  17. jspdf插件实现jsp页面导出为pdf文件
  18. 快递100获取快递信息(爬虫)
  19. Java学习之常用的Java构建工具
  20. 驱动蓝屏代码及原因,解决方案

热门文章

  1. python 求出4行5列的二维数组周边元素之和
  2. etl工程师 面试题_数据仓库工程师面试题笔试.doc
  3. php+mockjs,mockjs的常用方法分享
  4. 双飞翼布局内容不换行_web前端入门到实战:圣杯布局和双飞翼布局
  5. linux grub rescue 光盘,Ubuntu9.10用安装光盘如何进入linux rescue方式?
  6. batchsize大小对训练速度的影响
  7. Linux Vi的使用
  8. 大型网站的HTTPS实践:基于协议和配置的优化
  9. 第一章、第一节 Angular基础
  10. Degree Sequence of Graph G【模拟】