简体   繁体   中英

tornado.locks.Lock release

The tornado example gives the following example for locks:

>>> from tornado import gen, locks
>>> lock = locks.Lock()
>>>
>>> @gen.coroutine
... def f():
...    with (yield lock.acquire()):
...        # Do something holding the lock.
...        pass
...
...    # Now the lock is released.

Do you need to release the lock manually after the with or is that the purpose of using the with statement in that block? If this is the case why is there a separate release() and does this function need to be yielded?

Right, the with statement ensures the Lock is released, no need to call release yourself.

release is naturally non-blocking -- your coroutine can complete a call to release without waiting for any other coroutines -- therefore release does not require a yield statement. You can determine that for yourself by noticing the return value of release is None , whereas the return value of acquire is a Future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM