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.pswp.cn/news/541322.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541322.shtml
英文地址,請注明出處:http://en.pswp.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;后來發…