academy.handle¶
Handle
¶
Handle(
agent_id: AgentId[AgentT],
*,
exchange: ExchangeClient[Any] | None = None,
ignore_context: bool = False
)
Handle to a remote agent.
Internally, handles use an
ExchangeClient
to send requests to
and receive responses from the remote agent. By default the correct
exchange client is inferred from the context using a
context variable (specifically, the
academy.handle.exchange_context
variable). This allows the same handle
to be used in different contexts, automatically using the correct client
to send messages.
When a handle is used in contexts that have not configured the exchange
client (such as outside of an agent runtime or
Manager
), a default exchange can be provided
via the exchange
argument. For advanced usage, the ignore_context
flag
will cause the handle to only use the exchange
argument no matter what
the current context is.
Note
The exchange
argument will not be included when a handle is pickled.
Thus, unpickled handles must be used in a context that configures
an exchange client.
Parameters:
-
agent_id
(AgentId[AgentT]
) –ID of the remote agent.
-
exchange
(ExchangeClient[Any] | None
, default:None
) –A default exchange client to be used if an exchange client is not configured in the current context.
-
ignore_context
(bool
, default:False
) –Ignore the current context and force use of
exchange
for communication.
Raises:
-
ValueError
–If
ignore_context=True
butexchange
is not provided.
Source code in academy/handle.py
exchange
property
¶
exchange: ExchangeClient[Any]
Exchange client used to send messages.
Returns:
-
ExchangeClient[Any]
–Exchange client.
Raises:
-
ExchangeClientNotFoundError
–If no exchange client is set in the current context nor was one provided to the handle.
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:
-
R
–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).
-
Exception
–Any exception raised by the action.
Source code in academy/handle.py
ping
async
¶
Ping the agent.
Ping the agent and wait to get a response.
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).
-
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 request. Any unexpected error responses sent by the exchange will be logged.
Parameters:
-
terminate
(bool | None
, default:None
) –Override the termination behavior 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).
Source code in academy/handle.py
ProxyHandle
¶
ProxyHandle(agent: AgentT)
Proxy handle.
A proxy handle is thin wrapper around an
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
exchange
property
¶
exchange: ExchangeClient[Any]
Exchange client used to send messages.
Returns:
-
ExchangeClient[Any]
–Exchange client.
Raises:
-
ExchangeClientNotFoundError
–If no exchange client is set in the current context nor was one provided to the handle.
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:
-
R
–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).
-
Exception
–Any exception raised by the action.
Source code in academy/handle.py
ping
async
¶
Ping the agent.
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).
-
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 behavior 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).