academy.exchange¶
Exchange
¶
Bases: Protocol
Message exchange client protocol.
A message exchange hosts mailboxes for each entity (i.e., agent or client) in a multi-agent system. This protocol defines the client interface to an arbitrary exchange.
Warning
Exchange implementations should be efficiently pickleable so that agents and remote clients can establish client connections to the same exchange.
close
¶
register_agent
¶
register_agent(
behavior: type[BehaviorT],
*,
agent_id: AgentId[BehaviorT] | None = None,
name: str | None = None
) -> AgentId[BehaviorT]
Create a new agent identifier and associated mailbox.
Parameters:
-
behavior
(type[BehaviorT]
) –Behavior type of the agent.
-
agent_id
(AgentId[BehaviorT] | None
, default:None
) –Specify the ID of the agent. Randomly generated default.
-
name
(str | None
, default:None
) –Optional human-readable name for the agent. Ignored if
agent_id
is provided.
Returns:
-
AgentId[BehaviorT]
–Unique identifier for the agent's mailbox.
Source code in academy/exchange/__init__.py
register_client
¶
terminate
¶
terminate(uid: EntityId) -> None
Close the mailbox for an entity from the exchange.
Note
This method is a no-op if the mailbox does not exist.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.
discover
¶
Discover peer agents with a given behavior.
Parameters:
-
behavior
(type[Behavior]
) –Behavior type of interest.
-
allow_subclasses
(bool
, default:True
) –Return agents implementing subclasses of the behavior.
Returns:
Source code in academy/exchange/__init__.py
get_handle
¶
get_handle(
aid: AgentId[BehaviorT],
) -> UnboundRemoteHandle[BehaviorT]
Create a new handle to an agent.
A handle enables a client to invoke actions on the agent.
Note
It is not possible to create a handle to a client since a handle is essentially a new client of a specific agent.
Parameters:
-
aid
(AgentId[BehaviorT]
) –EntityId of the agent to create a handle to.
Returns:
-
UnboundRemoteHandle[BehaviorT]
–Handle to the agent.
Raises:
-
BadEntityIdError
–if a mailbox for
aid
does not exist. -
TypeError
–if
aid
is not an instance ofAgentId
.
Source code in academy/exchange/__init__.py
get_mailbox
¶
send
¶
Send a message to a mailbox.
Parameters:
Raises:
-
BadEntityIdError
–if a mailbox for
uid
does not exist. -
MailboxClosedError
–if the mailbox was closed.
Source code in academy/exchange/__init__.py
ExchangeMixin
¶
Mixin class that adds basic methods to an exchange implementation.
This adds a simple repr
/str
, context manager support, and the
register_agent
, register_client
, and get_handle
methods.
get_handle
¶
get_handle(
aid: AgentId[BehaviorT],
) -> UnboundRemoteHandle[BehaviorT]
Create a new handle to an agent.
A handle enables a client to invoke actions on the agent.
Note
It is not possible to create a handle to a client since a handle is essentially a new client of a specific agent.
Parameters:
-
aid
(AgentId[BehaviorT]
) –EntityId of the agent to create an handle to.
Returns:
-
UnboundRemoteHandle[BehaviorT]
–Handle to the agent.
Raises:
Source code in academy/exchange/__init__.py
Mailbox
¶
Bases: Protocol
Client protocol that listens to incoming messages to a mailbox.
close
¶
Close this mailbox client.
Warning
This does not close the mailbox in the exchange. I.e., the exchange will still accept new messages to this mailbox, but this client will no longer be listening for them.
Source code in academy/exchange/__init__.py
recv
¶
Receive the next message in the mailbox.
This blocks until the next message is received or the mailbox is closed.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the next message. If
None
, the default, block forever until the next message or the mailbox is closed.
Raises:
-
MailboxClosedError
–if the mailbox was closed.
-
TimeoutError
–if a
timeout
was specified and exceeded.