class PromiseActorRef(ActorRef): def __init__(self): super(PromiseActorRef, self).__init__(None) self.promise = Promise() def tell(self, message, sender=None): self.promise.complete(message)
class Dispatcher(object): throughput = 5 def __init__(self, executor): self._executor = executor self._attach_lock = Lock() self._attached_count = 0 self._terminated = Promise() def dispatch(self, message, mailbox): if not mailbox.is_closed(): mailbox.enqueue(message) self.schedule_execution(mailbox) else: print("Failed to deliver message. mailbox closed") def dispatch_system(self, message, mailbox): if not mailbox.is_closed(): mailbox.enqueue_system(message) self.schedule_execution(mailbox) else: print("Failed to deliver system message. mailbox closed") def attach(self, mailbox): with self._attach_lock: self._attached_count += 1 assert not mailbox.is_closed() def detach(self, mailbox): assert mailbox.is_closed() with self._attach_lock: self._attached_count -= 1 if self._attached_count == 0: self._terminated.complete(None) self._executor.shutdown() def schedule_execution(self, mailbox): if mailbox.is_closed() or mailbox.is_scheduled( ) or not mailbox.has_messages(): return if mailbox.set_scheduled(): self._executor.submit(mailbox.process_messages)
class Dispatcher(object): throughput = 5 def __init__(self, executor): self._executor = executor self._attach_lock = Lock() self._attached_count = 0 self._terminated = Promise() def dispatch(self, message, mailbox): if not mailbox.is_closed(): mailbox.enqueue(message) self.schedule_execution(mailbox) else: print("Failed to deliver message. mailbox closed") def dispatch_system(self, message, mailbox): if not mailbox.is_closed(): mailbox.enqueue_system(message) self.schedule_execution(mailbox) else: print("Failed to deliver system message. mailbox closed") def attach(self, mailbox): with self._attach_lock: self._attached_count += 1 assert not mailbox.is_closed() def detach(self, mailbox): assert mailbox.is_closed() with self._attach_lock: self._attached_count -= 1 if self._attached_count == 0: self._terminated.complete(None) self._executor.shutdown() def schedule_execution(self, mailbox): if mailbox.is_closed() or mailbox.is_scheduled() or not mailbox.has_messages(): return if mailbox.set_scheduled(): self._executor.submit(mailbox.process_messages)