- 论坛徽章:
- 0
|
本帖最后由 西风烈啸 于 2014-10-16 11:32 编辑
如下,定义了一个Condition新式类:
class Condition(object):
def __init__(self, lock=None):
self._lock = lock or RLock()
self._sleeping_count = Semaphore(0)
self._woken_count = Semaphore(0)
self._wait_semaphore = Semaphore(0)
self._make_methods()
……
try:
# wait for notification or timeout
self._wait_semaphore.acquire(True, timeout)
finally:
# indicate that this thread has woken
self._woken_count.release()
# reacquire lock
for i in xrange(count):
self._lock.acquire()
定义了一个Event新式类,
class Event(object):
def __init__(self):
self._cond = Condition(Lock()) #这里Condition继承Lock类吗?为什么在实例化的时候继承而不是定义的时候继承?还是我理解有错?
self._flag = Semaphore(0)
def is_set(self):
self._cond.acquire()
try:
if self._flag.acquire(False):
self._flag.release()
return True
return False
finally:
self._cond.release()
……
那位大神知道? |
|