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
exchangefor communication.
Raises:
-
ValueError–If
ignore_context=Truebutexchangeis 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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
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).