academy.handle¶
Handle
¶
Agent handle protocol.
A handle enables an agent or user to invoke actions on another agent.
action
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
close
async
¶
ping
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination agent of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
HandleDict
¶
HandleDict(
values: (
Mapping[K, Handle[AgentT]]
| Iterable[tuple[K, Handle[AgentT]]]
) = (),
/,
**kwargs: dict[str, Handle[AgentT]],
)
HandleList
¶
List of handles.
Tip
The HandleList
is required when storing a list of handles as
attributes of a Agent
so that those handles get bound to the
correct agent when running.
Source code in academy/handle.py
ProxyHandle
¶
ProxyHandle(agent: AgentT)
Proxy handle.
A proxy handle is thin wrapper around a
Agent
instance that is useful for testing
agents 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
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
close
async
¶
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
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination agent of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
UnboundRemoteHandle
¶
Handle to a remote agent that not bound to a mailbox.
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:
Source code in academy/handle.py
bind_to_client
¶
bind_to_client(
client: ExchangeClient[Any],
) -> RemoteHandle[AgentT]
Bind the handle to an existing mailbox.
Parameters:
-
client
(ExchangeClient[Any]
) –Exchange client.
Returns:
-
RemoteHandle[AgentT]
–Remote handle bound to the exchange client.
Source code in academy/handle.py
action
async
¶
Raises HandleNotBoundError
.
close
async
¶
Raises HandleNotBoundError
.
ping
async
¶
Raises HandleNotBoundError
.
RemoteHandle
¶
RemoteHandle(
exchange: ExchangeClient[Any], agent_id: AgentId[AgentT]
)
Handle to a remote agent bound to an exchange client.
Parameters:
-
exchange
(ExchangeClient[Any]
) –Exchange client used for agent communication.
-
agent_id
(AgentId[AgentT]
) –EntityId of the target agent of this handle.
Source code in academy/handle.py
clone
¶
clone() -> UnboundRemoteHandle[AgentT]
close
async
¶
Close this handle.
Note
This does not close the exchange client.
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
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
Source code in academy/handle.py
ping
async
¶
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:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.
-
TimeoutError
–If the timeout is exceeded.
Source code in academy/handle.py
shutdown
async
¶
shutdown(*, terminate: bool | None = None) -> None
Instruct the agent to shutdown.
This is non-blocking and will only send the message.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination agent of the agent defined in the
RuntimeConfig
.
Raises:
-
AgentTerminatedError
–If the agent's mailbox was closed. This typically indicates the agent shutdown for another reason (it self terminated or via another handle).
-
HandleClosedError
–If the handle was closed.