academy.multiplex¶
MailboxMultiplexer
¶
MailboxMultiplexer(
mailbox_id: EntityId,
exchange: Exchange,
request_handler: Callable[[RequestMessage], None],
)
Bases: NoPickleMixin
Multiplex a single mailbox across many consumers.
A mailbox represents a recipient entity. In many cases, there may be many entities within a single process that want to send and receive messages. For example, a running agent may have multiple handles to other agents. A naive approach would be for the agent and each handle to have their own mailbox, but this requires a listening thread in the process for each mailbox. This does not scale well. The multiplexer lets multiple entities (e.g., an agent and its handles) share the a single mailbox so their is one listening thread and messages are dispatched to the appropriate entity (i.e., object) within the process.
Note
This class should not be considered as a part of the public API. It
is used internally by other components, such as the
Agent
and
Manager
,
which use multiple handles concurrently.
Parameters:
-
mailbox_id
(EntityId
) –EntityId of the mailbox to multiplex. For example, the identifier of an agent.
-
exchange
(Exchange
) –The exchange interface managing the mailbox.
-
request_handler
(Callable[[RequestMessage], None]
) –A callable invoked when the request message is received to the inbox.
Source code in academy/multiplex.py
bind
¶
bind(
handle: RemoteHandle[BehaviorT],
) -> BoundRemoteHandle[BehaviorT]
Bind a handle to this mailbox.
Parameters:
-
handle
(RemoteHandle[BehaviorT]
) –Remote handle to bind.
Returns:
-
BoundRemoteHandle[BehaviorT]
–Remote handle bound to this mailbox.
Source code in academy/multiplex.py
close
¶
Close the multiplexer.
Closes all handles bound to this mailbox and then closes the mailbox.
close_bound_handles
¶
Close all handles bound to this mailbox.
Source code in academy/multiplex.py
terminate
¶
listen
¶
Listen for new messages in the mailbox and process them.
Request messages are processed via the request_handler
, and response
messages are dispatched to the handle that created the corresponding
request.
Warning
This method loops forever, until the mailbox is closed. Thus this method is typically run inside of a thread.
Note
Response messages intended for a handle that does not exist will be logged and discarded.