suppress channel notifications for ui background tasks

This commit is contained in:
jxxghp
2026-04-29 23:13:57 +08:00
parent 0b7505a604
commit 7ab643d34a
6 changed files with 133 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
import contextvars
from contextlib import contextmanager
from typing import Iterator
_suppress_message_channel = contextvars.ContextVar(
"suppress_message_channel", default=False
)
def is_message_channel_suppressed() -> bool:
"""
当前上下文是否禁止向外部消息渠道派发通知。
"""
return bool(_suppress_message_channel.get())
@contextmanager
def suppress_message_channel() -> Iterator[None]:
"""
在当前上下文中临时禁用外部消息渠道派发。
"""
token = _suppress_message_channel.set(True)
try:
yield
finally:
_suppress_message_channel.reset(token)