本文整理汇总了Python中gevent.lock.BoundedSemaphore.locked方法的典型用法代码示例。如果您正苦于以下问题:Python BoundedSemaphore.locked方法的具体用法?Python BoundedSemaphore.locked怎么用?Python BoundedSemaphore.locked使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.lock.BoundedSemaphore
的用法示例。
在下文中一共展示了BoundedSemaphore.locked方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _LeakySemaphore
# 需要导入模块: from gevent.lock import BoundedSemaphore [as 别名]
# 或者: from gevent.lock.BoundedSemaphore import locked [as 别名]
class _LeakySemaphore(object):
def __init__(self, value = 1, maxSeconds = 10):
self._semaphore = BoundedSemaphore(value)
self._maxSeconds = maxSeconds
self._timer = None
self._leaked = 0
self._stopped = False
def _leak(self):
sleep(self._maxSeconds)
self._leaked += 1
self._semaphore.release()
@property
def inUse(self):
return self._semaphore._initial_value - self.semaphore.counter
@property
def waiting(self):
return len(self._semaphore._links)
def release(self):
if self._stopped:
return
if self._leaked > 0:
self._leaked -= 1
else:
self._semaphore.release()
def stop(self):
self._stopped = True
if self._timer is not None:
self._timer.kill(block = False)
self._timer = None
while self.waiting > 0:
self._semaphore.release()
sleep(0.1)
def acquire(self):
if self._stopped:
return
if self._semaphore.locked() and not self._timer:
self._timer = spawn(self._leak)
self._semaphore.acquire(blocking = True, timeout = None)
if self._timer is not None:
self._timer.kill(block = False)
self._timer = None
if self.waiting > 0:
self._timer = spawn(self._leak)