本文整理汇总了Python中threading.RLock.locked方法的典型用法代码示例。如果您正苦于以下问题:Python RLock.locked方法的具体用法?Python RLock.locked怎么用?Python RLock.locked使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.RLock
的用法示例。
在下文中一共展示了RLock.locked方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from threading import RLock [as 别名]
# 或者: from threading.RLock import locked [as 别名]
class Relax_lock:
"""A type of locking object for relax."""
def __init__(self, name='unknown', fake_lock=False):
"""Set up the lock-like object.
@keyword name: The special name for the lock, used in debugging.
@type name: str
@keyword fake_lock: A flag which is True will allow this object to be debugged as the locking mechanism is turned off.
@type fake_lock: bool
"""
# Store the args.
self.name = name
self._fake_lock = fake_lock
# Init a reentrant lock object.
self._lock = RLock()
# The status container.
self._status = Status()
# Fake lock.
if self._fake_lock:
# Track the number of acquires.
self._lock_level = 0
def acquire(self, acquirer='unknown'):
"""Simulate the RLock.acquire() mechanism.
@keyword acquirer: The optional name of the acquirer.
@type acquirer: str
"""
# Debugging.
if self._status.debug:
sys.stdout.write("debug> Lock '%s': Acquisition by '%s'.\n" % (self.name, acquirer))
# Fake lock.
if self._fake_lock:
# Increment the lock level.
self._lock_level += 1
# Throw an error.
if self._lock_level > 1:
raise
# Return to prevent real locking.
return
# Acquire the real lock.
lock = self._lock.acquire()
# Return the real lock.
return lock
def locked(self):
"""Simulate the RLock.locked() mechanism."""
# Call the real method.
return self._lock.locked()
def release(self, acquirer='unknown'):
"""Simulate the RLock.release() mechanism.
@keyword acquirer: The optional name of the acquirer.
@type acquirer: str
"""
# Debugging.
if self._status.debug:
sys.stdout.write("debug> Lock '%s': Release by '%s'.\n" % (self.name, acquirer))
# Fake lock.
if self._fake_lock:
# Decrement the lock level.
self._lock_level -= 1
# Return to prevent real lock release.
return
# Release the real lock.
release = self._lock.release()
# Return the status.
return release