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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

rocksdb ubuntu c++源码编译测试

2019独角兽企业重金招聘Python工程师标准>>> 环境&#xff1a; ubuntu16.4 需要安装 snappy gflage bz2 zstd 以及g 其中zstd是facebook开放源代码里的压缩的库 git clone https://github.com/facebook/rocksdb.git cd rocksdb make static_lib 成功生成 librocksd…

vs生成linux服务器程序,从Visual Studio到Linux上调试C++代码

从Visual Studio到Linux上调试C代码04/30/20155 分钟可看完本文内容[原文发表时间] 2015/4/29 10:00 PM正如您可能已经听说的那样&#xff0c;Visual Studio 2015新推出了对Android开发的GDB支持。有趣的是&#xff0c;因为这项功能依赖GDB调试&#xff0c;我们完全可能稍加改动…

java clock计时_Java Clock类| 实例的Instant()方法

java clock计时Clock Class Instant()方法 (Clock Class instant() method) instant() method is available in java.time package. Instant()方法在java.time包中可用。 instant() method is used to get the current instant that is used with this Clock. Instant()方法用于…

使用国内DOCKER镜像源

在国内&#xff0c;通过Docker的pull和push命令访问hub.docker时&#xff0c;网络十分慢&#xff0c;而且会出现各种各样的网络连接问题。因此这里介绍下如何使用国内的镜像源&#xff0c;这里以DaoCloud为例。注册DaoCloud用户;注册完成后&#xff0c;会进入dashboard页面&…

linux命令top查看进程,linux 查看进程的命令(top)

标签&#xff1a;top操作系统支持多任务并不是计算机同时做很多事情&#xff0c;而是快速的轮换着执行这些任务。linux安排不同的程序等待使用CPU。进程的工作方式&#xff1a;当系统启动的时候&#xff0c;内核先把自己的程序初始化为进程&#xff0c;然后运行一个init的程序&…

stl vector 函数_vector :: back()函数以及C ++ STL中的示例

stl vector 函数C vector :: back()函数 (C vector::back() function) vector::back() is a library function of "vector" header, it is used to access the last element from the vector, it returns a reference to the last element of the vector. vector ::…

linux udp 端口 抓包,tcpdump之UDP抓包

摘要使用tcpdump抓UDP包&#xff0c;过滤过滤IP和port&#xff0c;并且自动拆分片段。安装tcpdumpyum install -y tcpdump使用方法tcpdump -i bond0 udp port xxxx and host xxx.x.xx.xxx -s0 -G 600 -w %Y_%m%d_%H%M_%S.pcap参数说明-i 指定监听的网卡udp 监听UDP协议port 指定…

Vue版本过渡变化

到了2.0以后&#xff0c;有哪些变化&#xff1a; 在每个组件模板&#xff0c;不在支持片段代码之前: <template id”aaa”><h3>我是组件</h3><strong>我是加粗标签</strong></template> 现在: 必须有根元素&#xff0c;包裹住所有的代码 …

NABARD的完整形式是什么?

NABARD&#xff1a;国家农业和农村发展银行 (NABARD: National Bank for Agriculture and Rural Development) NABARD is an abbreviation of National Bank for Agriculture and Rural Development. NABARD是国家农业和农村发展银行的缩写 。 On 12 July 1982, it was establ…

基于opencv+Dlib的面部合成(Face Morph)

引自&#xff1a;http://blog.csdn.net/wangxing233/article/details/51549880 零、前言 前段时间看到文章【1】和【2】&#xff0c;大概了解了面部合成的基本原理。这两天空下来了&#xff0c;于是参考【3】自己实现了下。虽然【1】和【2】已经讲的很清楚了&#xff0c;但是有…

大脑应对危机的模式_危机的完整形式是什么?

大脑应对危机的模式危机&#xff1a;印度信用评级信息服务有限公司 (CRISIL: Credit Rating Information Services of India Limited) CRISIL is an abbreviation of Credit Rating Information Services of India Limited. It is an international analytic company which off…

linux网络延迟命令,2. Linux使用ping命令查看网络延迟

ping命令持续发送少量互联网流量到远程地址并报告收到回应的总时间。如果流量因为网络故障或者错误配置而被丢弃&#xff0c;它也会报告。ping命令是最基本和初级的诊断网络问题的工具之一。ping常被用来测试网络延迟&#xff0c;但是有时ping的延迟并不是网络引起的&#xff0…

一、简单工厂模式

# public class Operation //基类{private double _numberA 0;private double _numberB 0;public double NumberA{get{ return _numberA; }set{_numberA value;}}public double NumberB{get{ return _numberB; }set{_numberB value;}}public virtual double GetResult(){d…

软件生命周期模型及其类型

A life cycle model is also known as a process model. As the name suggests, the software life cycle model (or the software process model) gives us a pictorial representation of the entire software development process. 生命周期模型也称为过程模型 。 顾名思义&…

linux查看磁盘io带宽,[Linux] 磁盘IO性能查看和优化以及iostat命令

iostat命令:%user&#xff1a;CPU处在用户模式下的时间百分比。%nice&#xff1a;CPU处在带NICE值的用户模式下的时间百分比。%system&#xff1a;CPU处在系统模式下的时间百分比。%iowait&#xff1a;CPU等待输入输出完成时间的百分比。%steal&#xff1a;管理程序维护另一个虚…

Jsoup 数据修改

2019独角兽企业重金招聘Python工程师标准>>> 1 设置属性的值 在解析一个Document之后可能想修改其中的某些属性值&#xff0c;然后再保存到磁盘或都输出到前台页面。 可以使用属性设置方法 Element.attr(String key, String value), 和 Elements.attr(String key, S…

软件静态架构 软件组件图_组件图| 软件工程

软件静态架构 软件组件图什么是组件图&#xff1f; (What is Component Diagram?) A Component Diagram breaks down the real system under development into different heights of working. Every component is reactive for the main aim in the entire system and only re…

如何卸载非linux系统分区,如何卸载Linux系统分区?卸载Linux系统分区的方法-站长资讯中心...

系统为windows xp sp2和redhat as 5双系统&#xff0c;其中linux系统后安装的在D盘&#xff0c;华彩软件站www.huacolor.com小编今天发现硬盘不够用了&#xff0c;想干掉linux分区&#xff0c;在虚拟机中用linux。就在windows的磁盘管理(命令为:diskmgmt)下删除linux分区&#…

顺序结构复习

复习一些易错知识点还有习题 目录 可能不熟悉的知识点 逻辑表达式的求解 if,else的配队 条件运算符 运算符优先级的问题 switch的使用 goto和if构成的循环 例题讲解 1 2 3 4 ​编辑 5 ​编辑 6赋值 ​编辑 7 可能不熟悉的知识点 逻辑表达式的求解 如果…

模板模式(部分方法延迟到子类实现)

项目中&#xff0c;用到了抽象类作为父类&#xff0c;有部分实现。 提供了了模板方法作为子类公共方法&#xff0c;模板方法中调用了抽象类的抽象方法和部分非抽象方法。 执行代码时&#xff0c;发现模板方法调用了抽象类的抽象方法&#xff0c;当时比较好奇&#xff0c;后来发…