academy.exchange.redis¶
RedisExchange
¶
Bases: ExchangeMixin
Redis-hosted message exchange interface.
Parameters:
-
hostname
(str
) –Redis server hostname.
-
port
(int
) –Redis server port.
-
kwargs
(Any
, default:{}
) –Extra keyword arguments to pass to
redis.Redis()
. -
timeout
(int | None
, default:None
) –Timeout for waiting on the next message. If
None
, the timeout will be set to one second but will loop indefinitely.
Raises:
-
ConnectionError
–If the Redis server is not reachable.
Source code in academy/exchange/redis.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/redis.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/redis.py
terminate
¶
terminate(uid: EntityId) -> None
Close the mailbox for an entity from the exchange.
Note
This method is a no-op if the mailbox does not exist.
Parameters:
-
uid
(EntityId
) –Entity identifier of the mailbox to close.
Source code in academy/exchange/redis.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/redis.py
get_mailbox
¶
get_mailbox(uid: EntityId) -> RedisMailbox
Get a client to a specific mailbox.
Parameters:
-
uid
(EntityId
) –EntityId of the mailbox.
Returns:
-
RedisMailbox
–Mailbox client.
Raises:
-
BadEntityIdError
–if a mailbox for
uid
does not exist.
Source code in academy/exchange/redis.py
send
¶
Send a message to a mailbox.
Parameters:
Raises:
-
BadEntityIdError
–if a mailbox for
uid
does not exist. -
MailboxClosedError
–if the mailbox was closed.
Source code in academy/exchange/redis.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
RedisMailbox
¶
RedisMailbox(uid: EntityId, exchange: RedisExchange)
Bases: NoPickleMixin
Client protocol that listens to incoming messages to a mailbox.
Parameters:
-
uid
(EntityId
) –EntityId of the mailbox.
-
exchange
(RedisExchange
) –Exchange client.
Raises:
-
BadEntityIdError
–if a mailbox with
uid
does not exist.
Source code in academy/exchange/redis.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/redis.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.