fix(runtime): 收口调度与事件异步任务生命周期 (#6415)

This commit is contained in:
InfinityPacer
2026-08-23 17:17:59 +08:00
committed by GitHub
parent e7bfc9dd43
commit e1d7918297
18 changed files with 1937 additions and 214 deletions
+14 -4
View File
@@ -29,6 +29,7 @@ class EventDispatcher:
event_loop: Callable[[], Any],
event_factory: Callable[..., Any],
error_handler: Callable[..., None],
async_handle_sink: Callable[[Any], bool] | None = None,
) -> None:
"""注入注册表、绑定器、执行器和错误策略回调。"""
self._registry = registry
@@ -37,6 +38,7 @@ class EventDispatcher:
self._event_loop = event_loop
self._event_factory = event_factory
self._error_handler = error_handler
self._async_handle_sink = async_handle_sink
def dispatch_chain(self, event: Any) -> bool:
"""同步按优先级顺序执行链式事件快照。"""
@@ -119,10 +121,18 @@ class EventDispatcher:
correlation_id=event.correlation_id,
)
if inspect.iscoroutinefunction(handler):
asyncio.run_coroutine_threadsafe(
self.safe_invoke_async(handler, isolated),
self._event_loop(),
)
coroutine = self.safe_invoke_async(handler, isolated)
if self._async_handle_sink:
self._async_handle_sink(coroutine)
continue
try:
asyncio.run_coroutine_threadsafe(coroutine, self._event_loop())
except RuntimeError:
coroutine.close()
logger.warning(
"事件 %s 的异步处理器无法投递,事件循环已停止",
event.event_type,
)
else:
self._executor().submit(
self.safe_invoke_sync,