agentscope.app.message_busInMemoryMessageBus
class InMemoryMessageBus(MessageBus)
agentscope/app/message_bus/_in_memory_message_bus.py:30–538In 记忆消息总线,把智能体接入外部消息渠道与企业平台。位于 agentscope.app.message_bus 包,继承自 MessageBus。核心成员包括 aclose、queue_push、queue_drain、queue_delete、log_append、log_read。
Mapping of bus modes to in-memory structures:
- Mode A (drain queue) — each key maps to a
:class:
list[tuple[str, dict]]of(entry_id, payload)pairs.queue_pushappends;queue_drainpops from the front (FIFO) and deletes the returned entries. - Mode C (replay log) — same underlying list structure, but
log_readis non-destructive.log_trimremoves entries in-place. - Mode D (transient broadcast) — each channel keeps a set of
:class:
asyncio.Queuesubscribers.publishpushes to all currently-subscribed queues;subscribeyields from one. - Mode E (distributed lock) — :class:
asyncio.Lockper key, suitable only for single-process concurrency. - Mode F (registry map) —
dict[str, dict[str, str]], one nested dict per namespace.
Entry ids are monotonic "<seq>-0" strings (e.g. "1-0",
"2-0", …). They are not lexicographically sortable once
the sequence exceeds single digits; comparison must parse the
numeric prefix (as :meth:log_read does internally).
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__() -> None | 初始化 `InMemoryMessageBus` 实例。 Initialise empty in-memory stores. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def __aenter__() -> Selfasync | 进入异步上下文并返回自身。 No-op — nothing to open for in-memory transport. |
async def aclose() -> Noneasync | 返回 `aclose` 结果。 Signal all open subscribers so their generators terminate. |
async def queue_push(key: str, payload: dict, *, ttl_secs: int | None = None) -> strasync | 执行队列推送相关操作。 Append ``payload`` to the in-memory drain queue at ``key``. |
async def queue_drain(key: str, max_count: int = 100) -> list[tuple[str, dict]]async | 执行 `queue_drain` 方法。 Drain up to ``max_count`` entries from the queue at ``key``. |
async def queue_delete(key: str) -> Noneasync | 设置队列删除。 Delete the drain queue at ``key``. |
async def log_append(key: str, payload: dict, *, ttl_secs: int | None = None, max_len: int | None = None) -> strasync | 执行日志追加相关操作。 Append ``payload`` to the replay log at ``key``. |
async def log_read(key: str, since: str | None = None, max_count: int = 100) -> list[tuple[str, dict]]async | 执行日志读取相关操作。 Read up to ``max_count`` entries newer than ``since``. |
async def log_trim(key: str, before_id: str | None = None) -> Noneasync | 执行日志裁剪相关操作。 Trim the replay log at ``key``. |
async def publish(key: str, payload: dict) -> Noneasync | 执行发布相关操作。 Publish ``payload`` to all current subscribers of ``key``. |
async def subscribe(key: str, *, on_ready: Callable[[], None] | None = None) -> AsyncGenerator[dict, None]async | 执行订阅相关操作。 Yield broadcast payloads for ``key`` until the consumer closes the generator. |
async def acquire_lock(key: str, *, ttl_secs: int = 600) -> AsyncGenerator[None, None]asyncasynccontextmanager | 执行 `acquire_lock` 方法。 Acquire a process-local mutex on ``key``. |
async def is_locked(key: str) -> boolasync | 判断是否 `is_locked`。 Return whether ``key`` currently holds a lock. |
async def try_lock(key: str, *, ttl_secs: int = 600) -> boolasync | 执行 `try_lock` 方法。 Non-blocking claim on ``key``. See base. |
async def unlock(key: str) -> Noneasync | 设置该参数。 Release a ``try_lock`` claim. |
async def registry_set(namespace: str, field: str, value: str, *, ttl_secs: int | None = None) -> Noneasync | 设置`field`到`value`in 注册表 at`namespace`。 Set ``field`` to ``value`` in the registry at ``namespace``. |
async def registry_set_if(namespace: str, field: str, value: str, *, expected: str, ttl_secs: int | None = None) -> boolasync | 执行注册表设置 if相关操作。 Compare-and-set one field; ``ttl_secs`` ignored. See base. |
async def registry_pop(namespace: str, field: str) -> str | Noneasync | 执行 `registry_pop` 方法。 Read and remove one field. See base. |
async def registry_del(namespace: str, field: str) -> Noneasync | 执行 `registry_del` 方法。 Remove ``field`` from the registry at ``namespace``. |
async def registry_exists(namespace: str, field: str) -> boolasync | 执行 `registry_exists` 方法。 Return whether ``field`` exists in the registry at ``namespace``. |
async def registry_getall(namespace: str) -> dict[str, str]async | 设置该参数。 Return all field-value pairs in the registry at ``namespace``. |
async def registry_get(namespace: str, field: str) -> str | Noneasync | 执行注册表读取相关操作。 Return a single field value from the registry at ``namespace``, or ``None`` if absent. |
async def registry_drop(namespace: str) -> Noneasync | 设置该参数。 Delete the entire registry at ``namespace``. |
构造器详情Constructor Detail
InMemoryMessageBus
def __init__() -> None
初始化 InMemoryMessageBus 实例。
Initialise empty in-memory stores.
agentscope/app/message_bus/_in_memory_message_bus.py:58–87方法详情Method Detail
__aenter__
async def __aenter__() -> Self
进入异步上下文并返回自身。
No-op — nothing to open for in-memory transport.
agentscope/app/message_bus/_in_memory_message_bus.py:93–100aclose
async def aclose() -> None
返回 aclose 结果。
Signal all open subscribers so their generators terminate.
agentscope/app/message_bus/_in_memory_message_bus.py:102–107queue_push
async def queue_push(key: str, payload: dict, *, ttl_secs: int | None = None) -> str
执行队列推送相关操作。
When ttl_secs is set the entry expires after that many seconds; expired entries are dropped here and on drain, so a never-drained key stays bounded.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Queue identifier. |
payload | dict | 负载(类型 `dict`)。JSON-serializable dict to enqueue. |
ttl_secskw默认default None | int | None | 存活时间 secs(类型 `int | None`)。Entry lifetime in seconds; `None` never expires. |
agentscope/app/message_bus/_in_memory_message_bus.py:130–161queue_drain
async def queue_drain(key: str, max_count: int = 100) -> list[tuple[str, dict]]
执行 queue_drain 方法。
Returned entries are removed from the internal list.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Queue identifier. |
max_count默认default 100 | int | 最大数量(类型 `int`)。Maximum entries to return. |
agentscope/app/message_bus/_in_memory_message_bus.py:163–189queue_delete
async def queue_delete(key: str) -> None
设置队列删除。
Delete the drain queue at key.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Queue identifier. |
agentscope/app/message_bus/_in_memory_message_bus.py:191–198log_append
async def log_append(key: str, payload: dict, *, ttl_secs: int | None = None, max_len: int | None = None) -> str
执行日志追加相关操作。
Append payload to the replay log at key.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Log identifier. |
payload | dict | 负载(类型 `dict`)。JSON-serializable dict to append. |
ttl_secskw默认default None | int | None | 存活时间 secs(类型 `int | None`)。Ignored (no-op — in-memory does not expire keys). |
max_lenkw默认default None | int | None | 最大 len(类型 `int | None`)。If set, trim the log to approximately this many entries after appending. |
agentscope/app/message_bus/_in_memory_message_bus.py:204–234log_read
async def log_read(key: str, since: str | None = None, max_count: int = 100) -> list[tuple[str, dict]]
执行日志读取相关操作。
Reads are non-destructive.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Log identifier. |
since默认default None | str | None | since(类型 `str | None`)。Exclusive cursor — return entries whose id is strictly greater than `since`. `None` reads from the start. |
max_count默认default 100 | int | 最大数量(类型 `int`)。Maximum entries to return. |
agentscope/app/message_bus/_in_memory_message_bus.py:236–276log_trim
async def log_trim(key: str, before_id: str | None = None) -> None
执行日志裁剪相关操作。
Trim the replay log at key.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Log identifier. |
before_id默认default None | str | None | 之前标识(类型 `str | None`)。Drop all entries with id strictly older than this. `None` drops the entire log. |
agentscope/app/message_bus/_in_memory_message_bus.py:278–301publish
async def publish(key: str, payload: dict) -> None
执行发布相关操作。
Publish payload to all current subscribers of key.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Channel identifier. |
payload | dict | 负载(类型 `dict`)。JSON-serializable dict delivered to each subscriber. |
agentscope/app/message_bus/_in_memory_message_bus.py:307–321subscribe
async def subscribe(key: str, *, on_ready: Callable[[], None] | None = None) -> AsyncGenerator[dict, None]
执行订阅相关操作。
Yield broadcast payloads for key until the consumer closes the generator.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Channel identifier. |
on_readykw默认default None | Callable[[], None] | None | on 就绪(类型 `Callable[[], None] | None`)。Called once after the subscription is established and before any payload is yielded. |
agentscope/app/message_bus/_in_memory_message_bus.py:323–355acquire_lock
async def acquire_lock(key: str, *, ttl_secs: int = 600) -> AsyncGenerator[None, None]
执行 acquire_lock 方法。
In-memory equivalent of a distributed lock.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Lock identifier. |
ttl_secskw默认default 600 | int | 存活时间 secs(类型 `int`)。Ignored (no automatic expiry). |
agentscope/app/message_bus/_in_memory_message_bus.py:362–389is_locked
async def is_locked(key: str) -> bool
判断是否 is_locked。
Return whether key currently holds a lock.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。Lock identifier. |
agentscope/app/message_bus/_in_memory_message_bus.py:391–402try_lock
async def try_lock(key: str, *, ttl_secs: int = 600) -> bool
执行 try_lock 方法。
Non-blocking claim on key.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。The key. |
ttl_secskw默认default 600 | int | 存活时间 secs(类型 `int`)。The ttl secs. |
agentscope/app/message_bus/_in_memory_message_bus.py:404–411unlock
async def unlock(key: str) -> None
设置该参数。
Release a try_lock claim.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
key | str | 键(类型 `str`)。The key. |
agentscope/app/message_bus/_in_memory_message_bus.py:413–415registry_set
async def registry_set(namespace: str, field: str, value: str, *, ttl_secs: int | None = None) -> None
设置field到valuein 注册表 atnamespace。
ttl_secs is accepted for API compatibility but ignored.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key. |
field | str | field(类型 `str`)。Field name. |
value | str | 值(类型 `str`)。Value to store. |
ttl_secskw默认default None | int | None | 存活时间 secs(类型 `int | None`)。Ignored (no TTL support). |
agentscope/app/message_bus/_in_memory_message_bus.py:421–443registry_set_if
async def registry_set_if(namespace: str, field: str, value: str, *, expected: str, ttl_secs: int | None = None) -> bool
执行注册表设置 if相关操作。
Compare-and-set one field; ttl_secs ignored.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。The namespace. |
field | str | field(类型 `str`)。The field. |
value | str | 值(类型 `str`)。The value. |
expectedkw | str | expected(类型 `str`)。The expected. |
ttl_secskw默认default None | int | None | 存活时间 secs(类型 `int | None`)。The ttl secs. |
agentscope/app/message_bus/_in_memory_message_bus.py:445–459registry_pop
async def registry_pop(namespace: str, field: str) -> str | None
执行 registry_pop 方法。
Read and remove one field.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。The namespace. |
field | str | field(类型 `str`)。The field. |
agentscope/app/message_bus/_in_memory_message_bus.py:461–463registry_del
async def registry_del(namespace: str, field: str) -> None
执行 registry_del 方法。
Remove field from the registry at namespace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key. |
field | str | field(类型 `str`)。Field to remove. |
agentscope/app/message_bus/_in_memory_message_bus.py:465–476registry_exists
async def registry_exists(namespace: str, field: str) -> bool
执行 registry_exists 方法。
Return whether field exists in the registry at namespace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key. |
field | str | field(类型 `str`)。Field to check. |
agentscope/app/message_bus/_in_memory_message_bus.py:478–492registry_getall
async def registry_getall(namespace: str) -> dict[str, str]
设置该参数。
Return all field-value pairs in the registry at namespace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key. |
agentscope/app/message_bus/_in_memory_message_bus.py:494–509registry_get
async def registry_get(namespace: str, field: str) -> str | None
执行注册表读取相关操作。
Return a single field value from the registry at namespace, or None if absent.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key. |
field | str | field(类型 `str`)。Field to retrieve. |
agentscope/app/message_bus/_in_memory_message_bus.py:511–529registry_drop
async def registry_drop(namespace: str) -> None
设置该参数。
Delete the entire registry at namespace.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
namespace | str | 命名空间(类型 `str`)。Registry key to delete. |
agentscope/app/message_bus/_in_memory_message_bus.py:531–538继承成员Inherited Members
MessageBus__aexit__, session_run, session_is_running, session_publish_event, session_read_events, session_subscribe_events, session_publish_cancel, session_subscribe_cancel, session_purge, inbox_push, inbox_drain, enqueue_wakeup, enqueue_input, dequeue_wakeups, subscribe_wakeup_signal, bg_task_register, bg_task_unregister, bg_task_exists, bg_task_list, bg_task_purge, task_publish_cancel, task_subscribe_cancel