consumer decorator
Just stumbled across this little gem, and I don’t want to forget about it. Here
is a decorator that takes care of the ugliness in first call to .next()
,
necessary for receiving coroutines in Python.
def consumer(func):
def start(*args,**kwargs):
c = func(*args,**kwargs)
c.next()
return c
return start
@consumer
def recv_count():
try:
while True:
n = (yield) # Yield expression
print "T-minus", n
except GeneratorExit:
print "Kaboom!"