diff --git a/vnpy/api/rest/RestClient.py b/vnpy/api/rest/RestClient.py index 031c6214..fa3ef0d4 100644 --- a/vnpy/api/rest/RestClient.py +++ b/vnpy/api/rest/RestClient.py @@ -7,7 +7,7 @@ from multiprocessing.dummy import Pool import requests from enum import Enum -from typing import Any, Callable +from typing import Any, Callable, Optional ######################################################################## @@ -151,16 +151,20 @@ class RestClient(object): #---------------------------------------------------------------------- def _run(self): - session = self._createSession() - while self._active: - try: - request = self._queue.get(timeout=1) + try: + session = self._createSession() + while self._active: try: - self._processRequest(request, session) - finally: - self._queue.task_done() - except Empty: - pass + request = self._queue.get(timeout=1) + try: + self._processRequest(request, session) + finally: + self._queue.task_done() + except Empty: + pass + except: + et, ev, tb = sys.exc_info() + self.onError(et, ev, tb, None) #---------------------------------------------------------------------- def sign(self, request): # type: (Request)->Request @@ -181,9 +185,15 @@ class RestClient(object): sys.stderr.write(str(request)) #---------------------------------------------------------------------- - def onError(self, exceptionType, exceptionValue, tb, request): + def onError(self, + exceptionType, # type: type + exceptionValue, # type: Exception + tb, + request # type: Optional[Request] + ): """ Python内部错误处理:默认行为是仍给excepthook + :param request 如果是在处理请求的时候出错,它的值就是对应的Request,否则为None """ print("error in request : {}\n".format(request)) sys.excepthook(exceptionType, exceptionValue, tb) diff --git a/vnpy/api/websocket/WebsocketClient.py b/vnpy/api/websocket/WebsocketClient.py index a2e73382..a029bda5 100644 --- a/vnpy/api/websocket/WebsocketClient.py +++ b/vnpy/api/websocket/WebsocketClient.py @@ -51,7 +51,6 @@ class WebsocketClient(object): #---------------------------------------------------------------------- def start(self): """启动""" - self._connect() self._active = True self._workerThread = Thread(target=self._run) @@ -130,29 +129,35 @@ class WebsocketClient(object): """ 运行,直到stop()被调用 """ + try: + self._connect() - # todo: onDisconnect - while self._active: - try: - ws = self._getWs() - if ws: - stream = ws.recv() - if not stream: # recv在阻塞的时候ws被关闭 - self._reconnect() - continue - - try: - data = json.loads(stream) - except ValueError as e: - print('websocket unable to parse data: ' + stream) - raise e - self.onPacket(data) - except websocket.WebSocketConnectionClosedException: # 在调用recv之前ws就被关闭了 - self._reconnect() - except: # Python内部错误(onPacket内出错) - et, ev, tb = sys.exc_info() - self.onError(et, ev, tb) - self._reconnect() + # todo: onDisconnect + while self._active: + try: + ws = self._getWs() + if ws: + stream = ws.recv() + if not stream: # recv在阻塞的时候ws被关闭 + self._reconnect() + continue + + try: + data = json.loads(stream) + except ValueError as e: + print('websocket unable to parse data: ' + stream) + raise e + self.onPacket(data) + except websocket.WebSocketConnectionClosedException: # 在调用recv之前ws就被关闭了 + self._reconnect() + except: # Python内部错误(onPacket内出错) + et, ev, tb = sys.exc_info() + self.onError(et, ev, tb) + self._reconnect() + except: + et, ev, tb = sys.exc_info() + self.onError(et, ev, tb) + self._reconnect() #---------------------------------------------------------------------- def _runPing(self):