academy.handle¶
Handle
¶
Handle(
agent_id: AgentId[AgentT_co] | None = None,
*,
exchange: ExchangeClient[Any] | None = None,
ignore_context: bool = False,
request_serializer: SerializationStrategy | None = None,
result_serializer: SerializationStrategy | None = None,
exception_serializer: (
SerializationStrategy | None
) = None,
polling_interval: float = 60,
reject_on_inactive: bool = False
)
Bases: Generic[AgentT_co]
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_co] | None, default:None) –ID of the remote agent, or
Noneto defer binding (used bybatch.queue()). -
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. -
request_serializer(SerializationStrategy | None, default:None) –Strategy used to serialize arguments. If None, use the
academy.serialize.default_serializer -
result_serializer(SerializationStrategy | None, default:None) –Strategy used to serialize results. If false-y, use the same strategy as the request serializer.
-
exception_serializer(SerializationStrategy | None, default:None) –Strategy used to serialize results. If false-y, use the same strategy as the result serializer.
-
polling_interval(float, default:60) –Interval to poll exchange to see if agent is active.
-
reject_on_inactive(bool, default:False) –Return an error when target agent is inactive.
Raises:
-
ValueError–If
ignore_context=Truebutexchangeis not provided.
Source code in academy/handle.py
reject_on_inactive
property
writable
¶
reject_on_inactive: bool
Reject actions when agent is inactive.
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.
__setstate__
¶
Set state.
This is necessary for unpickling to not treat set state as a remote action.
Source code in academy/handle.py
agent_stats
async
¶
agent_stats() -> AgentStats
agent_status
async
¶
agent_status() -> MailboxStatus
Return live status of the agent mailbox.
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:
-
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).
-
AgentInactiveError–If the agent is inactive (missed heartbeats) and handle is configured to reject actions when agent is inactive (reject_on_inactive=True)
-
Exception–Any exception raised by the action.
Source code in academy/handle.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
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 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).
Source code in academy/handle.py
ProxyHandle
¶
Bases: Handle[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
reject_on_inactive
property
writable
¶
reject_on_inactive: bool
Reject actions when agent is inactive.
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).
Source code in academy/handle.py
__setstate__
¶
Set state.
This is necessary for unpickling to not treat set state as a remote action.
Source code in academy/handle.py
agent_stats
async
¶
agent_stats() -> AgentStats
agent_status
async
¶
agent_status() -> MailboxStatus
Return live status of the agent mailbox.