日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

acquire方法_Python锁类| 带有示例的acquire()方法

發布時間:2023/12/1 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 acquire方法_Python锁类| 带有示例的acquire()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

acquire方法

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

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

acquisition()是Python中線程模塊的Lock類的內置方法。

This method is used to acquire a lock, either blocking or non-blocking. When it is invoked without arguments, it blocks the calling thread until the lock is unlocked by the thread using it currently.

此方法用于獲取鎖定,無論是鎖定還是非鎖定。 在不帶參數的情況下調用它時,它將阻塞調用線程,直到當前使用它的線程將鎖解鎖。

Module:

模塊:

from threading import Lock

Syntax:

句法:

acquire( blocking=True, timeout=-1)

Parameter(s):

參數:

  • blocking: It is an optional parameter, which works as a blocking flag. If it is set to True, the calling thread will be blocked if some other thread is holding the flag and once that lock is released, then the calling thread will acquire the lock and return True. If it is set to False, it will not block the thread if the lock is already acquired by some other thread, and will return False. Its default value is True.

    阻塞 :這是一個可選參數,用作阻塞標志。 如果將其設置為True,則如果其他某個線程持有該標志,則調用線程將被阻塞,并且一旦釋放該鎖,則調用線程將獲取該鎖并返回True。 如果將其設置為False,則如果其他線程已經獲取了鎖,它將不會阻塞線程,并且將返回False。 其默認值為True。

  • 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.

    timeout :這是一個可選參數,它指定如果其他某種方法當前正在獲取鎖,則阻塞調用線程的秒數。 它的默認值是-1,表示線程將無限期地阻塞,直到獲得鎖為止。

Note: It is forbidden to specify a timeout when blocking is false.

注意 :禁止在false為false時指定超時。

Return value:

返回值:

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

此方法的返回類型為<class'bool'> 。 該函數用于在實現多線程時獲取鎖,如果成功獲取鎖,則返回True,否則返回False 。

Example:

例:

# Python program to show # the use of acquire() method in Lock class import threading import randomclass shared(object):def __init__(self, x = 0):# Created a Lock objectself.lock = threading.Lock()self.incr = x# Increment function for the threaddef incrementcounter(self):print("Waiting for the lock to be unlocked")# Lock acquired by the current threadself.lock.acquire()try:print('Lock acquired, current counter value: ', self.incr)self.incr = self.incr + 1finally:print('Lock released, current counter value: ', self.incr)# Lock released by the given threadself.lock.release()def helper_thread(c):# Getting a random integer between 1 to 3r = 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

輸出量

Random value selected: 1 Waiting for the lock to be unlocked Lock acquired, current counter value: 0 Lock released, current counter value: 1 Finished Thread-1Random value selected: 2 Waiting for the lock to be unlocked Lock acquired, current counter value: 1 Lock released, current counter value: 2 Waiting for the lock to be unlocked Lock acquired, current counter value: 2 Lock released, current counter value: 3 Finished Thread-2Final counter value: 3

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

acquire方法

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的acquire方法_Python锁类| 带有示例的acquire()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。