def acquire(self, blocking=True, timeout=-1):
"""Must be used with 'yield' as 'yield cv.acquire()'.
"""
if not self._scheduler:
self._scheduler = Pycos.scheduler()
task = Pycos.cur_task(self._scheduler)
if self._owner == task:
self._depth += 1
raise StopIteration(True)
if not blocking and self._owner is not None:
raise StopIteration(False)
if timeout < 0:
timeout = None
while self._owner is not None:
if timeout is not None:
if timeout <= 0:
raise StopIteration(False)
start = _time()
self._waitlist.append(task)
if (yield task._await_(timeout)) is None:
try:
self._waitlist.remove(task)
except ValueError:
pass
if timeout is not None:
timeout -= (_time() - start)
assert self._depth == 0
self._owner = task
self._depth = 1
raise StopIteration(True)
评论列表
文章目录