From f17e6537f96c51907fbe0056118491e541052cd9 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sat, 30 Nov 2019 10:40:45 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA]=20=E5=A2=9E=E5=8A=A0debug?= =?UTF-8?q?=E9=80=89=E9=A1=B9=EF=BC=8C=E5=AF=B9=E6=89=A7=E8=A1=8C=E8=B6=85?= =?UTF-8?q?=E6=97=B6handler=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/event/engine.py | 47 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/vnpy/event/engine.py b/vnpy/event/engine.py index b7576d59..6a7854c4 100644 --- a/vnpy/event/engine.py +++ b/vnpy/event/engine.py @@ -1,11 +1,11 @@ """ Event-driven framework of vn.py framework. """ - +import sys from collections import defaultdict from queue import Empty, Queue from threading import Thread -from time import sleep +from time import sleep, time from typing import Any, Callable EVENT_TIMER = "eTimer" @@ -37,14 +37,20 @@ class EventEngine: which can be used for timing purpose. """ - def __init__(self, interval: int = 1): + def __init__(self, interval: int = 1, debug: bool = False, over_ms: int = 500): """ Timer event is generated every 1 second by default, if interval not specified. + 增强模式: + debug: performance debug + over_ms: over micro seconds for each handler execution. + add try catch handel event exception """ self._interval = interval self._queue = Queue() self._active = False + self._debug = debug + self._over_ms = over_ms self._thread = Thread(target=self._run) self._timer = Thread(target=self._run_timer) self._handlers = defaultdict(list) @@ -57,10 +63,43 @@ class EventEngine: while self._active: try: event = self._queue.get(block=True, timeout=1) - self._process(event) + self._process(event) if not self._debug else self._process_debug(event) except Empty: pass + def _process_debug(self, event: Event): + """ + process event with debug mode: + 1.performance + 2.try catch exception + + """ + for handler in self._handlers[event.type]: + t1 = time() + handler_name = str(handler.__qualname__) + try: + handler(event) + except Exception as ex: + print(f'运行 {event.type} {handler_name} 异常:{str(ex)}', + file=sys.stderr) + continue + t2 = time() + execute_ms = (int(round(t2 * 1000))) - (int(round(t1 * 1000))) + if execute_ms > self._over_ms: + print(f'运行{event.type} {handler_name} 耗时:{execute_ms}ms >{self._over_ms}ms', + file=sys.stderr) + + if self._general_handlers: + for handler in self._general_handlers: + t1 = time() + handler_name = str(handler.__qualname__) + handler(event) + t2 = time() + execute_ms = (int(round(t2 * 1000))) - (int(round(t1 * 1000))) + if execute_ms > self._over_ms: + print(f'运行 general {event.type} {handler_name} 耗时:{execute_ms}ms > {self._over_ms}ms', + file=sys.stderr) + def _process(self, event: Event): """ First ditribute event to those handlers registered listening