1 from __future__ import print_function
3 from eventlet import event as _event
6 class metaphore(object):
7 """This is sort of an inverse semaphore: a counter that starts at 0 and
8 waits only if nonzero. It's used to implement a "wait for all" scenario.
10 >>> from eventlet import coros, spawn_n
11 >>> count = coros.metaphore()
13 >>> def decrementer(count, id):
14 ... print("{0} decrementing".format(id))
17 >>> _ = spawn_n(decrementer, count, 'A')
18 >>> _ = spawn_n(decrementer, count, 'B')
27 self.event = _event.Event()
28 # send() right away, else we'd wait on the default 0 count!
32 """Increment our counter. If this transitions the counter from zero to
33 nonzero, make any subsequent :meth:`wait` call wait.
37 if self.counter == by:
38 # If we just incremented self.counter by 'by', and the new count
39 # equals 'by', then the old value of self.counter was 0.
40 # Transitioning from 0 to a nonzero value means wait() must
45 """Decrement our counter. If this transitions the counter from nonzero
46 to zero, a current or subsequent wait() call need no longer wait.
51 # Don't leave self.counter < 0, that will screw things up in
54 # Transitioning from nonzero to 0 means wait() need no longer wait.
58 """Suspend the caller only if our count is nonzero. In that case,
59 resume the caller once the count decrements to zero again.