Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
def test_equality():
    cell_1 = object()
    cell_2 = object()
    assert ActorRef(cell_1) != ActorRef(cell_2)
    assert ActorRef(cell_1) == ActorRef(cell_1)
    assert InternalRef(cell_1) != InternalRef(cell_2)
    assert InternalRef(cell_1) == InternalRef(cell_1)
    assert InternalRef(cell_1) == ActorRef(cell_1)
    assert ActorRef(cell_1) != object()
Exemplo n.º 3
0
class ActorSystem(actors.internal.factory.ActorFactory):

    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)

    def terminate(self):
        self._guardian.send_system_message(Terminate)
        self._terminate_promise.future.get()

    @property
    def dead_letters(self):
        return self._dead_letters

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.terminate()
Exemplo n.º 4
0
    def actor_of(self, cls=None, behaviour=None, dispatcher=None):
        if cls:
            factory = cls
        elif behaviour:
            factory = _actor_from_behaviour(behaviour)
        else:
            raise ValueError()

        if dispatcher is None:
            dispatcher = self._system._system_dispatcher

        from actors.internal.cell import Cell
        cell = Cell(factory, dispatcher=dispatcher, system=self._system,
                         parent=self._supervisor)

        internal_ref = InternalRef(cell)
        self._supervisor.send_system_message(Supervise(internal_ref))
        internal_ref.send_system_message(Start)

        return ActorRef(cell)
Exemplo n.º 5
0
    def actor_of(self, cls=None, behaviour=None, dispatcher=None):
        if cls:
            factory = cls
        elif behaviour:
            factory = _actor_from_behaviour(behaviour)
        else:
            raise ValueError()

        if dispatcher is None:
            dispatcher = self._system._system_dispatcher

        from actors.internal.cell import Cell
        cell = Cell(factory,
                    dispatcher=dispatcher,
                    system=self._system,
                    parent=self._supervisor)

        internal_ref = InternalRef(cell)
        self._supervisor.send_system_message(Supervise(internal_ref))
        internal_ref.send_system_message(Start)

        return ActorRef(cell)
Exemplo n.º 6
0
def test_supervising_actors_finalize_after_children_have_terminated(
        actor, cell, mailbox, supervisor, dispatcher):
    child = Mock(spec_set=InternalRef)
    cell.handle_system_message(Supervise(child))

    cell.handle_system_message(Terminate)
    assert not dispatcher.detach.called
    assert not supervisor.send_system_message.called
    assert not actor.post_stop.called

    cell.handle_system_message(Terminated(child))
    dispatcher.detach.assert_called_once_with(mailbox)
    supervisor.send_system_message.assert_called_once_with(
        Terminated(InternalRef(cell)))
    actor.post_stop.assert_called_once_with()
Exemplo n.º 7
0
    def __init__(self, actor_factory, system, dispatcher, parent):
        self._actor_factory = actor_factory  # Creates actor instances
        self._actor = None
        self._system = system
        self._dispatcher = dispatcher
        self._mailbox = InternalMailbox(dispatcher, self,
                                        dispatcher.throughput)
        self._dispatcher.attach(self._mailbox)

        self._parent = parent
        self._children = []
        self._stopped = False
        self._self_ref = InternalRef(self)

        self._context = actors.context.ActorContext(system, self._self_ref)
        self._context.system = system

        # TODO: shouldn't expose the internal ref
        self._context.self_ref = self._self_ref
Exemplo n.º 8
0
def test_terminate_sends_terminated_message_to_supervisor(cell, supervisor):
    cell.handle_system_message(Terminate)
    supervisor.send_system_message.assert_called_once_with(
        Terminated(InternalRef(cell)))