class Warehouse(Actor): handles = Actor.make_handles() def __init__(self): Actor.__init__(self) self.inventory = list() @handles(GetInventory) def on_get_inventory(self, message, sender): sender.send(list(self.inventory), self) @handles(OtherMessage) def on_task_result(self, result, sender): self.inventory.append(result) worker = Worker.spawn(lambda x: x * 2) positives = Warehouse.spawn() negatives = Warehouse.spawn() bridge = Bridge() for var in [1, 2, 3, -2, -4, -6]: warehouse = positives if var >= 0 else negatives worker.send(Task(var, warehouse), sender=None) print bridge.call(positives, GetInventory(), 1.0) print bridge.call(negatives, GetInventory(), 1.0) print bridge.stop([worker, positives, negatives], 1.0)
class Pong: def __repr__(self): return 'Pong()' class Pinger(Actor): handles = Actor.make_handles() @handles(Start) def on_start(self, start, sender): start.target.send(Ping(), self) @handles(Pong) def on_pong(self, pong, sender): print "-", sender.send(Ping(), self) class Ponger(Actor): handles = Actor.make_handles() @handles(Ping) def on_ping(self, ping, sender): print "+", sender.send(Pong(), self) bridge = Bridge() pinger = Pinger.spawn() ponger = Ponger.spawn() pinger.send(Start(ponger), sender=None) time.sleep(0.1) bridge.stop([pinger, ponger], 1)