mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 16:07:01 +08:00
fix async event
This commit is contained in:
+30
-10
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
import random
|
import random
|
||||||
@@ -71,15 +72,26 @@ class EventManager(metaclass=Singleton):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__executor = ThreadHelper() # 动态线程池,用于消费事件
|
# 动态线程池,用于消费事件
|
||||||
self.__consumer_threads = [] # 用于保存启动的事件消费者线程
|
self.__executor = ThreadHelper()
|
||||||
self.__event_queue = PriorityQueue() # 优先级队列
|
# 用于保存启动的事件消费者线程
|
||||||
self.__broadcast_subscribers: Dict[EventType, Dict[str, Callable]] = {} # 广播事件的订阅者
|
self.__consumer_threads = []
|
||||||
self.__chain_subscribers: Dict[ChainEventType, Dict[str, tuple[int, Callable]]] = {} # 链式事件的订阅者
|
# 优先级队列
|
||||||
self.__disabled_handlers = set() # 禁用的事件处理器集合
|
self.__event_queue = PriorityQueue()
|
||||||
self.__disabled_classes = set() # 禁用的事件处理器类集合
|
# 广播事件的订阅者
|
||||||
self.__lock = threading.Lock() # 线程锁
|
self.__broadcast_subscribers: Dict[EventType, Dict[str, Callable]] = {}
|
||||||
self.__event = threading.Event() # 退出事件
|
# 链式事件的订阅者
|
||||||
|
self.__chain_subscribers: Dict[ChainEventType, Dict[str, tuple[int, Callable]]] = {}
|
||||||
|
# 禁用的事件处理器集合
|
||||||
|
self.__disabled_handlers = set()
|
||||||
|
# 禁用的事件处理器类集合
|
||||||
|
self.__disabled_classes = set()
|
||||||
|
# 线程锁
|
||||||
|
self.__lock = threading.Lock()
|
||||||
|
# 退出事件
|
||||||
|
self.__event = threading.Event()
|
||||||
|
# 当前事件循环
|
||||||
|
self.loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""
|
"""
|
||||||
@@ -438,7 +450,15 @@ class EventManager(metaclass=Singleton):
|
|||||||
isolated_event = Event(event_type=event.event_type,
|
isolated_event = Event(event_type=event.event_type,
|
||||||
event_data=event_data_copy,
|
event_data=event_data_copy,
|
||||||
priority=event.priority)
|
priority=event.priority)
|
||||||
self.__executor.submit(self.__safe_invoke_handler, handler, isolated_event)
|
if inspect.iscoroutinefunction(handler):
|
||||||
|
# 对于异步函数,直接在事件循环中运行
|
||||||
|
asyncio.run_coroutine_threadsafe(
|
||||||
|
self.__safe_invoke_handler_async(handler, isolated_event),
|
||||||
|
self.loop
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# 对于同步函数,在线程池中运行
|
||||||
|
self.__executor.submit(self.__safe_invoke_handler, handler, isolated_event)
|
||||||
|
|
||||||
def __safe_invoke_handler(self, handler: Callable, event: Event):
|
def __safe_invoke_handler(self, handler: Callable, event: Event):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user