class PromiseActorRef(ActorRef): def __init__(self): super(PromiseActorRef, self).__init__(None) self.promise = Promise() def tell(self, message, sender=None): self.promise.complete(message)
def __init__(self, system_dispatcher=None): """ The actor system is responsible for creating, configuring and stopping actors and dispatchers. Normally, only one system per application should be created. :param system_dispatcher: Override the dispatcher used by the system. This also acts as the default dispatcher for new actors. :type system_dispatcher: :class:`Dispatcher` """ self._system_dispatcher = Dispatcher(Executor()) \ if system_dispatcher is None else system_dispatcher self._dead_letters = _DeadLetterRef() self._terminate_promise = Promise() class Guardian(Actor): def __init__(me): me._logger = logging.getLogger(__name__) def receive(me, message): me._logger.warning("User receive called. This should not be happen.") def post_stop(me): self._terminate_promise.complete(None) self._guardian = InternalRef(actors.internal.cell.Cell(Guardian, dispatcher=self._system_dispatcher, system=self, parent=None)) self._guardian.send_system_message(Start) actors.internal.factory.ActorFactory.__init__(self, self, self._guardian)
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)
def __init__(self, executor): self._executor = executor self._attach_lock = Lock() self._attached_count = 0 self._terminated = Promise()
def __init__(self): super(PromiseActorRef, self).__init__(None) self.promise = Promise()