Your coroutines must be executed forever to handle consecutive calls, for example:
@coroutine def PlatinumCustomer(successor=None): while 1: # <---- this is missing from your coroutines cust = (yield) if cust.custtype == 'platinum': print "Platinum Customer" elif successor is not None: successor.send(cust)
And to process the type 'undefined, you need the last catch-all handler:
@coroutine def UndefinedCustomer(): while 1: cust = (yield) print "No such customer type '%s'" % cust.custtype
and add it to your pipeline:
pipeline = PlatinumCustomer(GoldCustomer(SilverCustomer(DiamondCustomer(UndefinedCustomer()))))
(The UndefinedCustomer end handler will also allow you to remove the "if successor code" code from your coroutines - everyone will have successors, except for the terminator, who knows that he is a terminator and will not name the successor.)
With these changes, I get this result from your tests:
Platinum Customer Gold Customer No such customer type 'undefined'
Also, why catch for StopIteration in HandleCustomer? This code should be sufficient:
def HandleCustomer(self): self.pipeline.send(self)
source share