academy.handle¶
Handle
¶
Bases: Protocol[BehaviorT]
Agent handle protocol.
A handle enables a client or agent to invoke actions on another agent.
action
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
Future[R]
–Future to the result of the action.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
close
¶
ping
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
¶
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
HandleDict
¶
HandleDict(
values: (
Mapping[K, Handle[BehaviorT]]
| Iterable[tuple[K, Handle[BehaviorT]]]
) = (),
/,
**kwargs: dict[str, Handle[BehaviorT]],
)
HandleList
¶
ProxyHandle
¶
Bases: Generic[BehaviorT]
Proxy handle.
A proxy handle is thin wrapper around a
Behavior
instance that is useful for testing
behaviors that are initialized with a handle to another agent without
needing to spawn agents. This wrapper invokes actions synchronously.
Source code in academy/handle.py
action
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
Future[R]
–Future to the result of the action.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
close
¶
Close this handle.
Note
This is a no-op for proxy handles.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
ping
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Note
This is a no-op for proxy handles and returns 0 latency.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
¶
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
RemoteHandle
¶
RemoteHandle(
exchange: Exchange,
agent_id: AgentId[BehaviorT],
mailbox_id: EntityId | None = None,
)
Bases: Generic[BehaviorT]
, ABC
Handle to a remote agent.
This is an abstract base class with three possible concrete types representing the three remote handle states: unbound, client bound, or mailbox bound.
An unbound handle does not have a mailbox for itself which means it
cannot send messages to the remote agent because the handle does not
know the return address to use. Thus, unbound handles need to be bound
with bind_as_client
or bind_to_mailbox
before they can be used.
A client bound handle has a unique handle identifier and mailbox for itself. A mailbox bound handle shares its identifier and mailbox with another entity (e.g., a running agent).
Note
When an instance is pickled and unpickled, such as when
communicated along with an agent dispatched to run in another
process, the handle will be unpickled into an UnboundRemoteHandle
.
Thus, the handle will need to be bound before use.
Parameters:
-
exchange
(Exchange
) –Message exchange used for agent communication.
-
agent_id
(AgentId[BehaviorT]
) –EntityId of the target agent of this handle.
-
mailbox_id
(EntityId | None
, default:None
) –EntityId of the mailbox this handle receives messages to. If unbound, this is
None
.
Source code in academy/handle.py
bind_as_client
abstractmethod
¶
bind_as_client(
client_id: ClientId | None = None,
) -> ClientRemoteHandle[BehaviorT]
Bind the handle as a unique client in the exchange.
Note
This is an abstract method. Each remote handle variant implements different semantics.
Parameters:
-
client_id
(ClientId | None
, default:None
) –Client identifier to be used by this handle. If
None
, a new identifier will be created using the exchange.
Returns:
-
ClientRemoteHandle[BehaviorT]
–Remote handle bound to the client identifier.
Source code in academy/handle.py
bind_to_mailbox
abstractmethod
¶
bind_to_mailbox(
mailbox_id: EntityId,
) -> BoundRemoteHandle[BehaviorT]
Bind the handle to an existing mailbox.
Parameters:
-
mailbox_id
(EntityId
) –EntityId of the mailbox to bind to.
Returns:
-
BoundRemoteHandle[BehaviorT]
–Remote handle bound to the identifier.
Source code in academy/handle.py
close
¶
Close this handle.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
action
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
Future[R]
–Future to the result of the action.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
ping
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
¶
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
UnboundRemoteHandle
¶
Bases: RemoteHandle[BehaviorT]
Handle to a remote agent that is unbound.
Warning
An unbound handle must be bound before use. Otherwise all methods
will raise an HandleNotBoundError
when attempting to send a message
to the remote agent.
Parameters:
-
exchange
(Exchange
) –Message exchange used for agent communication.
-
agent_id
(AgentId[BehaviorT]
) –EntityId of the agent.
Source code in academy/handle.py
bind_as_client
¶
bind_as_client(
client_id: ClientId | None = None,
) -> ClientRemoteHandle[BehaviorT]
Bind the handle as a unique client in the exchange.
Parameters:
-
client_id
(ClientId | None
, default:None
) –Client identifier to be used by this handle. If
None
, a new identifier will be created using the exchange.
Returns:
-
ClientRemoteHandle[BehaviorT]
–Remote handle bound to the client identifier.
Source code in academy/handle.py
bind_to_mailbox
¶
bind_to_mailbox(
mailbox_id: EntityId,
) -> BoundRemoteHandle[BehaviorT]
Bind the handle to an existing mailbox.
Parameters:
-
mailbox_id
(EntityId
) –EntityId of the mailbox to bind to.
Returns:
-
BoundRemoteHandle[BehaviorT]
–Remote handle bound to the identifier.
Source code in academy/handle.py
action
¶
Raises HandleNotBoundError
.
ping
¶
Raises HandleNotBoundError
.
shutdown
¶
Raises HandleNotBoundError
.
close
¶
Close this handle.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
BoundRemoteHandle
¶
Bases: RemoteHandle[BehaviorT]
Handle to a remote agent bound to an existing mailbox.
Parameters:
-
exchange
(Exchange
) –Message exchange used for agent communication.
-
agent_id
(AgentId[BehaviorT]
) –EntityId of the target agent of this handle.
-
mailbox_id
(EntityId
) –EntityId of the mailbox this handle receives messages to.
Source code in academy/handle.py
bind_as_client
¶
bind_as_client(
client_id: ClientId | None = None,
) -> ClientRemoteHandle[BehaviorT]
Bind the handle as a unique client in the exchange.
Parameters:
-
client_id
(ClientId | None
, default:None
) –Client identifier to be used by this handle. If
None
, a new identifier will be created using the exchange.
Returns:
-
ClientRemoteHandle[BehaviorT]
–Remote handle bound to the client identifier.
Source code in academy/handle.py
bind_to_mailbox
¶
bind_to_mailbox(
mailbox_id: EntityId,
) -> BoundRemoteHandle[BehaviorT]
Bind the handle to an existing mailbox.
Parameters:
-
mailbox_id
(EntityId
) –EntityId of the mailbox to bind to.
Returns:
-
BoundRemoteHandle[BehaviorT]
–Remote handle bound to the identifier.
Source code in academy/handle.py
close
¶
Close this handle.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Source code in academy/handle.py
action
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
Future[R]
–Future to the result of the action.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
ping
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
¶
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
ClientRemoteHandle
¶
ClientRemoteHandle(
exchange: Exchange,
agent_id: AgentId[BehaviorT],
client_id: ClientId | None = None,
)
Bases: RemoteHandle[BehaviorT]
Handle to a remote agent bound as a unique client.
Parameters:
-
exchange
(Exchange
) –Message exchange used for agent communication.
-
agent_id
(AgentId[BehaviorT]
) –EntityId of the target agent of this handle.
-
client_id
(ClientId | None
, default:None
) –Client identifier of this handle. If
None
, a new identifier will be created using the exchange. Note this will become themailbox_id
attribute of the handle.
Source code in academy/handle.py
bind_as_client
¶
bind_as_client(
client_id: ClientId | None = None,
) -> ClientRemoteHandle[BehaviorT]
Bind the handle as a unique client in the exchange.
Parameters:
-
client_id
(ClientId | None
, default:None
) –Client identifier to be used by this handle. If
None
or equal to this handle's ID, self will be returned. Otherwise, a new identifier will be created using the exchange.
Returns:
-
ClientRemoteHandle[BehaviorT]
–Remote handle bound to the client identifier.
Source code in academy/handle.py
bind_to_mailbox
¶
bind_to_mailbox(
mailbox_id: EntityId,
) -> BoundRemoteHandle[BehaviorT]
Bind the handle to an existing mailbox.
Parameters:
-
mailbox_id
(EntityId
) –EntityId of the mailbox to bind to.
Returns:
-
BoundRemoteHandle[BehaviorT]
–Remote handle bound to the identifier.
Source code in academy/handle.py
close
¶
Close this handle.
Parameters:
-
wait_futures
(bool
, default:True
) –Wait to return until all pending futures are done executing. If
False
, pending futures are cancelled. -
timeout
(float | None
, default:None
) –Optional timeout used when
wait=True
.
Raises:
-
RuntimeError
–if the response message listener thread is not alive when
close()
is called indicating the listener thread likely crashed.
Source code in academy/handle.py
action
¶
Invoke an action on the agent.
Parameters:
-
action
(str
) –Action to invoke.
-
args
(Any
, default:()
) –Positional arguments for the action.
-
kwargs
(Any
, default:{}
) –Keywords arguments for the action.
Returns:
-
Future[R]
–Future to the result of the action.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
Source code in academy/handle.py
ping
¶
Ping the agent.
Ping the agent and wait to get a response. Agents process messages in order so the round-trip time will include processing time of earlier messages in the queue.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the response.
Returns:
-
float
–Round-trip time in seconds.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
¶
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Raises:
-
HandleClosedError
–If the handle was closed.
-
MailboxClosedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).