博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
acquire方法_Python锁类| 带有示例的acquire()方法
阅读量:2538 次
发布时间:2019-05-11

本文共 3655 字,大约阅读时间需要 12 分钟。

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.

注意 :禁止在falsefalse时指定超时。

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 classimport threadingimport 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()        try:            print('Lock acquired, current counter value: ', self.incr)            self.incr = self.incr + 1        finally:            print('Lock released, current counter value: ', self.incr)            # Lock released by the given thread            self.lock.release()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

输出量

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

翻译自:

acquire方法

转载地址:http://srxzd.baihongyu.com/

你可能感兴趣的文章
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>