academy.exchange¶
ExchangeFactory
¶
Bases: ABC
, Generic[ExchangeTransportT]
Exchange client factory.
An exchange factory is used to mint new exchange clients for users and
agents, encapsulating the complexities of instantiating the underlying
communication classes (the
ExchangeTransport
).
Warning
Factory implementations must be efficiently pickleable because factory instances are shared between user and agent processes so that all entities can create clients to the same exchange.
create_agent_client
async
¶
create_agent_client(
registration: AgentRegistration[AgentT],
request_handler: RequestHandler,
) -> AgentExchangeClient[AgentT, ExchangeTransportT]
Create a new agent exchange client.
An agent must be registered with the exchange before an exchange client can be created. For example:
factory = ExchangeFactory(...)
user_client = factory.create_user_client()
registration = user_client.register_agent(...)
agent_client = factory.create_agent_client(registration, ...)
Parameters:
-
registration
(AgentRegistration[AgentT]
) –Registration information returned by the exchange.
-
request_handler
(RequestHandler
) –Agent request message handler.
Returns:
-
AgentExchangeClient[AgentT, ExchangeTransportT]
–Agent exchange client.
Raises:
-
BadEntityIdError
–If an agent with
registration.agent_id
is not already registered with the exchange.
Source code in academy/exchange/__init__.py
create_user_client
async
¶
create_user_client(
*, name: str | None = None, start_listener: bool = True
) -> UserExchangeClient[ExchangeTransportT]
Create a new user in the exchange and associated client.
Parameters:
-
name
(str | None
, default:None
) –Display name of the client on the exchange.
-
start_listener
(bool
, default:True
) –Start a message listener thread.
Returns:
-
UserExchangeClient[ExchangeTransportT]
–User exchange client.
Source code in academy/exchange/__init__.py
ExchangeClient
¶
ExchangeClient(transport: ExchangeTransportT)
Bases: ABC
, Generic[ExchangeTransportT]
Base exchange client.
Warning
Exchange clients should only be created via
ExchangeFactory.create_agent_client()
or
ExchangeFactory.create_user_client()
!
Parameters:
-
transport
(ExchangeTransportT
) –Exchange transport bound to a mailbox.
Source code in academy/exchange/__init__.py
close
abstractmethod
async
¶
discover
async
¶
Discover peer agents with a given agent.
Parameters:
-
agent
(type[Agent]
) –Agent type of interest.
-
allow_subclasses
(bool
, default:True
) –Return agents implementing subclasses of the agent.
Returns:
Source code in academy/exchange/__init__.py
factory
¶
factory() -> ExchangeFactory[ExchangeTransportT]
get_handle
¶
get_handle(aid: AgentId[AgentT]) -> RemoteHandle[AgentT]
Create a new handle to an agent.
A handle acts like a reference to a remote agent, enabling a user to manage the agent or asynchronously invoke actions.
Parameters:
-
aid
(AgentId[AgentT]
) –Agent to create an handle to. The agent must be registered with the same exchange.
Returns:
-
RemoteHandle[AgentT]
–Handle to the agent.
Raises:
Source code in academy/exchange/__init__.py
register_agent
async
¶
register_agent(
agent: type[AgentT], *, name: str | None = None
) -> AgentRegistration[AgentT]
Register a new agent and associated mailbox with the exchange.
Parameters:
-
agent
(type[AgentT]
) –Agent type of the agent.
-
name
(str | None
, default:None
) –Optional display name for the agent.
Returns:
-
AgentRegistration[AgentT]
–Agent registration info.
Source code in academy/exchange/__init__.py
send
async
¶
send(message: Message) -> None
Send a message to a mailbox.
Parameters:
-
message
(Message
) –Message to send.
Raises:
-
BadEntityIdError
–If a mailbox for
message.dest
does not exist. -
MailboxTerminatedError
–If the mailbox was closed.
Source code in academy/exchange/__init__.py
status
async
¶
status(uid: EntityId) -> MailboxStatus
Check the status of a mailbox in the exchange.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to check.
terminate
async
¶
terminate(uid: EntityId) -> None
Terminate a mailbox in the exchange.
Terminating a mailbox means that the corresponding entity will no longer be able to receive messages.
Note
This method is a no-op if the mailbox does not exist.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.
Source code in academy/exchange/__init__.py
AgentExchangeClient
¶
AgentExchangeClient(
agent_id: AgentId[AgentT],
transport: ExchangeTransportT,
request_handler: RequestHandler,
)
Bases: ExchangeClient[ExchangeTransportT]
, Generic[AgentT, ExchangeTransportT]
Agent exchange client.
Warning
Agent exchange clients should only be created via
ExchangeFactory.create_agent_client()
!
Parameters:
-
agent_id
(AgentId[AgentT]
) –Agent ID.
-
transport
(ExchangeTransportT
) –Exchange transport bound to
agent_id
. -
request_handler
(RequestHandler
) –Request handler of the agent that will be called for each message received to this agent's mailbox. start_listener: Start a message listener thread.
Source code in academy/exchange/__init__.py
close
async
¶
Close the user client.
This closes the underlying exchange transport and all handles created by this client. The agent's mailbox will not be terminated so the agent can be started again later.
Source code in academy/exchange/__init__.py
discover
async
¶
Discover peer agents with a given agent.
Parameters:
-
agent
(type[Agent]
) –Agent type of interest.
-
allow_subclasses
(bool
, default:True
) –Return agents implementing subclasses of the agent.
Returns:
Source code in academy/exchange/__init__.py
factory
¶
factory() -> ExchangeFactory[ExchangeTransportT]
get_handle
¶
get_handle(aid: AgentId[AgentT]) -> RemoteHandle[AgentT]
Create a new handle to an agent.
A handle acts like a reference to a remote agent, enabling a user to manage the agent or asynchronously invoke actions.
Parameters:
-
aid
(AgentId[AgentT]
) –Agent to create an handle to. The agent must be registered with the same exchange.
Returns:
-
RemoteHandle[AgentT]
–Handle to the agent.
Raises:
Source code in academy/exchange/__init__.py
register_agent
async
¶
register_agent(
agent: type[AgentT], *, name: str | None = None
) -> AgentRegistration[AgentT]
Register a new agent and associated mailbox with the exchange.
Parameters:
-
agent
(type[AgentT]
) –Agent type of the agent.
-
name
(str | None
, default:None
) –Optional display name for the agent.
Returns:
-
AgentRegistration[AgentT]
–Agent registration info.
Source code in academy/exchange/__init__.py
send
async
¶
send(message: Message) -> None
Send a message to a mailbox.
Parameters:
-
message
(Message
) –Message to send.
Raises:
-
BadEntityIdError
–If a mailbox for
message.dest
does not exist. -
MailboxTerminatedError
–If the mailbox was closed.
Source code in academy/exchange/__init__.py
status
async
¶
status(uid: EntityId) -> MailboxStatus
Check the status of a mailbox in the exchange.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to check.
terminate
async
¶
terminate(uid: EntityId) -> None
Terminate a mailbox in the exchange.
Terminating a mailbox means that the corresponding entity will no longer be able to receive messages.
Note
This method is a no-op if the mailbox does not exist.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.
Source code in academy/exchange/__init__.py
UserExchangeClient
¶
UserExchangeClient(
user_id: UserId,
transport: ExchangeTransportT,
*,
start_listener: bool = True
)
Bases: ExchangeClient[ExchangeTransportT]
User exchange client.
Warning
User exchange clients should only be created via
ExchangeFactory.create_user_client()
!
Parameters:
-
user_id
(UserId
) –User ID.
-
transport
(ExchangeTransportT
) –Exchange transport bound to
user_id
. -
start_listener
(bool
, default:True
) –Start a message listener thread.
Source code in academy/exchange/__init__.py
close
async
¶
Close the user client.
This terminates the user's mailbox, closes the underlying exchange transport, and closes all handles produced by this client.
Source code in academy/exchange/__init__.py
discover
async
¶
Discover peer agents with a given agent.
Parameters:
-
agent
(type[Agent]
) –Agent type of interest.
-
allow_subclasses
(bool
, default:True
) –Return agents implementing subclasses of the agent.
Returns:
Source code in academy/exchange/__init__.py
factory
¶
factory() -> ExchangeFactory[ExchangeTransportT]
get_handle
¶
get_handle(aid: AgentId[AgentT]) -> RemoteHandle[AgentT]
Create a new handle to an agent.
A handle acts like a reference to a remote agent, enabling a user to manage the agent or asynchronously invoke actions.
Parameters:
-
aid
(AgentId[AgentT]
) –Agent to create an handle to. The agent must be registered with the same exchange.
Returns:
-
RemoteHandle[AgentT]
–Handle to the agent.
Raises:
Source code in academy/exchange/__init__.py
register_agent
async
¶
register_agent(
agent: type[AgentT], *, name: str | None = None
) -> AgentRegistration[AgentT]
Register a new agent and associated mailbox with the exchange.
Parameters:
-
agent
(type[AgentT]
) –Agent type of the agent.
-
name
(str | None
, default:None
) –Optional display name for the agent.
Returns:
-
AgentRegistration[AgentT]
–Agent registration info.
Source code in academy/exchange/__init__.py
send
async
¶
send(message: Message) -> None
Send a message to a mailbox.
Parameters:
-
message
(Message
) –Message to send.
Raises:
-
BadEntityIdError
–If a mailbox for
message.dest
does not exist. -
MailboxTerminatedError
–If the mailbox was closed.
Source code in academy/exchange/__init__.py
status
async
¶
status(uid: EntityId) -> MailboxStatus
Check the status of a mailbox in the exchange.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to check.
terminate
async
¶
terminate(uid: EntityId) -> None
Terminate a mailbox in the exchange.
Terminating a mailbox means that the corresponding entity will no longer be able to receive messages.
Note
This method is a no-op if the mailbox does not exist.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.