academy.exchange.hybrid¶
HybridExchange
¶
HybridExchange(
redis_host: str,
redis_port: int,
*,
interface: str | None = None,
namespace: str | None = "default",
redis_kwargs: dict[str, Any] | None = None
)
Bases: ExchangeMixin
Hybrid exchange.
The hybrid exchange uses peer-to-peer communication via TCP and a central Redis server for mailbox state and queueing messages for offline entities.
Parameters:
-
redis_host
(str
) –Redis server hostname.
-
redis_port
(int
) –Redis server port.
-
interface
(str | None
, default:None
) –Network interface use for peer-to-peer communication. If
None
, the hostname of the local host is used. -
namespace
(str | None
, default:'default'
) –Redis key namespace. If
None
a random key prefix is generated. -
redis_kwargs
(dict[str, Any] | None
, default:None
) –Extra keyword arguments to pass to
redis.Redis()
.
Raises:
-
ConnectionError
–If the Redis server is not reachable.
Source code in academy/exchange/hybrid.py
close
¶
register_agent
¶
register_agent(
behavior: type[BehaviorT],
*,
agent_id: AgentId[BehaviorT] | None = None,
name: str | None = None
) -> AgentId[BehaviorT]
Create a new agent identifier and associated mailbox.
Parameters:
-
behavior
(type[BehaviorT]
) –Type of the behavior this agent will implement.
-
agent_id
(AgentId[BehaviorT] | None
, default:None
) –Specify the ID of the agent. Randomly generated default.
-
name
(str | None
, default:None
) –Optional human-readable name for the agent. Ignored if
agent_id
is provided.
Returns:
-
AgentId[BehaviorT]
–Unique identifier for the agent's mailbox.
Source code in academy/exchange/hybrid.py
register_client
¶
Create a new client identifier and associated mailbox.
Parameters:
-
name
(str | None
, default:None
) –Optional human-readable name for the client.
Returns:
-
ClientId
–Unique identifier for the client's mailbox.
Source code in academy/exchange/hybrid.py
terminate
¶
terminate(uid: EntityId) -> None
Close the mailbox for an entity from the exchange.
This sets the state of the mailbox to inactive in the Redis server, and deletes any queued messages in Redis.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.
Source code in academy/exchange/hybrid.py
discover
¶
Discover peer agents with a given behavior.
Warning
This method is O(n) and scans all keys in the Redis server.
Parameters:
-
behavior
(type[Behavior]
) –Behavior type of interest.
-
allow_subclasses
(bool
, default:True
) –Return agents implementing subclasses of the behavior.
Returns:
Source code in academy/exchange/hybrid.py
get_mailbox
¶
get_mailbox(uid: EntityId) -> HybridMailbox
Get a client to a specific mailbox.
Parameters:
-
uid
(EntityId
) –EntityId of the mailbox.
Returns:
-
HybridMailbox
–Mailbox client.
Raises:
-
BadEntityIdError
–if a mailbox for
uid
does not exist.
Source code in academy/exchange/hybrid.py
send
¶
Send a message to a mailbox.
To send a message, the client first checks that the state of the mailbox in Redis is active; otherwise, an error is raised. Then, the client checks to see if the peer entity is available by checking for an address of the peer in Redis. If the peer's address is found, the message is sent directly to the peer via ZMQ; otherwise, the message is put in a Redis queue for later retrieval.
Parameters:
Raises:
-
BadEntityIdError
–if a mailbox for
uid
does not exist. -
MailboxClosedError
–if the mailbox was closed.
Source code in academy/exchange/hybrid.py
get_handle
¶
get_handle(
aid: AgentId[BehaviorT],
) -> UnboundRemoteHandle[BehaviorT]
Create a new handle to an agent.
A handle enables a client to invoke actions on the agent.
Note
It is not possible to create a handle to a client since a handle is essentially a new client of a specific agent.
Parameters:
-
aid
(AgentId[BehaviorT]
) –EntityId of the agent to create an handle to.
Returns:
-
UnboundRemoteHandle[BehaviorT]
–Handle to the agent.
Raises:
Source code in academy/exchange/__init__.py
HybridMailbox
¶
HybridMailbox(
uid: EntityId,
exchange: HybridExchange,
*,
interface: str | None = None,
port: int | None = None
)
Bases: NoPickleMixin
Client protocol that listens to incoming messages to a mailbox.
This class acts as the endpoint for messages sent to a particular mailbox. This is done via starting two threads once initialized: (1) a ZMQ server thread that listens for messages from peers, and (2) a thread that checks the Redis server for any offline messages and state changes to the mailbox (i.e., mailbox closure).
Parameters:
-
uid
(EntityId
) –EntityId of the mailbox.
-
exchange
(HybridExchange
) –Exchange client.
-
interface
(str | None
, default:None
) –Network interface use for peer-to-peer communication. If
None
, the hostname of the local host is used. -
port
(int | None
, default:None
) –Port to use for peer-to-peer communication. Randomly selected if
None
.
Source code in academy/exchange/hybrid.py
close
¶
Close this mailbox client.
Warning
This does not close the mailbox in the exchange. I.e., the exchange will still accept new messages to this mailbox, but this client will no longer be listening for them.
Source code in academy/exchange/hybrid.py
recv
¶
Receive the next message in the mailbox.
This blocks until the next message is received or the mailbox is closed.
Parameters:
-
timeout
(float | None
, default:None
) –Optional timeout in seconds to wait for the next message. If
None
, the default, block forever until the next message or the mailbox is closed. Note that this will be cast to an int which is required by the Redis API.
Raises:
-
MailboxClosedError
–if the mailbox was closed.
-
TimeoutError
–if a
timeout
was specified and exceeded.
Source code in academy/exchange/hybrid.py
base32_to_uuid
¶
Parse a base32 string as a UUID.
uuid_to_base32
¶
Encode a UUID as a trimmed base32 string.