Skip to content

PubSubHub

作用域限定在 Flet 服务进程内的、线程安全的进程内发布/订阅路由器。

订阅者按会话 ID 分组,也可按主题分组。事件处理器可以是同步可调用对象,也可以是异步协程函数:

  • 全局订阅者会收到 (message);
  • 主题订阅者会收到 (topic, message)。

已建立会话的 PubSubClient 实例会借助此中枢,在已连接的会话之间分发消息。

方法 ​

名称类型默认值说明
send_all——Sends a message to all global subscribers across all sessions. Args: message: Payload to deliver.
send_all_on_topic——Sends a topic message to all subscribers of topic. Args: topic: Topic name to broadcast on. message: Payload to deliver.
send_others——Sends a global message to all sessions except one. Args: except_session_id: Session ID to exclude from delivery. message: Payload to deliver.
send_others_on_topic——Sends a topic message to all subscribers except one session. Args: except_session_id: Session ID to exclude from delivery. topic: Topic name to publish on. message: Payload to deliver.
subscribe——Registers a global subscriber for a session. The handler will receive one positional argument: message. Duplicate registrations of the same handler are ignored because handlers are stored in a set. Args: session_id: Session identifier that owns this subscription. handler: Sync or async callback invoked for global messages.
subscribe_topic——Registers a topic subscriber for a session. The handler will receive two positional arguments: (topic, message). Args: session_id: Session identifier that owns this subscription. topic: Topic name to subscribe to. handler: Sync or async callback invoked for topic messages.
unsubscribe——Removes all global subscribers for a session. This does not remove topic subscriptions; use unsubscribe_all to remove both. Args: session_id: Session identifier to remove.
unsubscribe_topic——Removes all handlers for a specific session/topic pair. Args: session_id: Session identifier to remove from the topic. topic: Topic to unsubscribe from.
unsubscribe_all——Removes both global and topic subscriptions for a session. Args: session_id: Session identifier to fully unsubscribe.

关注我,学习大模型、大数据、Python等热门技术。