Actor messages

Actors communicate with each other by sending and receiving messages. The pulsar.async.mailbox module implements the message passing layer via a bidirectional socket connections between the Arbiter and any Actor.

Message sending is asynchronous and safe, the message is guaranteed to eventually reach the recipient, provided that the recipient exists.

The implementation details are outlined below:

  • Messages are sent via the send() function, which is a proxy for the actor send() method. Here is how you ping actor abc in a coroutine:

    from pulsar import send
    
    async def example():
        result = await send('abc', 'ping')
    
  • The Arbiter mailbox is a TcpServer accepting connections from remote actors.

  • The Actor.mailbox is a MailboxClient of the arbiter mailbox server.

  • When an actor sends a message to another actor, the arbiter mailbox behaves as a proxy server by routing the message to the targeted actor.

  • Communication is bidirectional and there is only one connection between the arbiter and any given actor.

  • Messages are encoded and decoded using the unmasked websocket protocol implemented in frame_parser().

  • If, for some reasons, the connection between an actor and the arbiter get broken, the actor will eventually stop running and garbaged collected.

Implementation

For the curious this is how the internal protocol is implemented

Protocol

class pulsar.async.mailbox.MailboxProtocol(**kw)[source]

The Protocol for internal message passing between actors.

Encoding and decoding uses the unmasked websocket protocol.

request(command, sender, target, args, kwargs)[source]

Used by the server to send messages to the client.

Client

class pulsar.async.mailbox.MailboxClient(address, actor, loop)[source]

Used by actors to send messages to other actors via the arbiter.

protocol_factory

alias of MailboxProtocol