Lock.acquire(False)可以吗? [英] Lock.acquire(False) does?

查看:564
本文介绍了Lock.acquire(False)可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码

    locked = lock.acquire(False)
        if locked:  break

根据python doc:
lock.aquire(False):- 在将阻塞参数设置为False的情况下调用时,请勿阻塞.如果将阻塞设置为True的呼叫将阻塞,则立即返回False;否则,请将锁定设置为锁定并返回True. 我很理解他们说的话,但是有人可以简化一下,请就上述代码向我解释.

according to python doc:
lock.aquire(False):- When invoked with the blocking argument set to False, do not block. If a call with blocking set to True would block, return False immediately; otherwise, set the lock to locked and return True. I quite understand what they said but can somebody simplify this and please explain me in relation to the above code.

推荐答案

默认情况下, lock.acquire 将阻塞线程的执行,直到其他线程释放该锁为止.如果将block=False传递给该函数(如您的示例所示),则该调用将不会阻塞,并会立即返回.它的返回值指定您的线程是否实际获得了锁.

By default, lock.acquire will block execution of the thread until the lock is released by a different thread. If you pass block=False to the function (as in your example), the call will not block, and will return immediately. Its return value specifies whether or not your thread has actually acquired the lock.

这篇关于Lock.acquire(False)可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆