From 759489b1fd46d4441bf6e3cbe098f2af11580c05 Mon Sep 17 00:00:00 2001 From: nanoric Date: Thu, 25 Oct 2018 04:22:36 -0400 Subject: [PATCH] =?UTF-8?q?[Mod]=20=E5=B0=86websocket.=5Fconnect=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=B0=E5=B7=A5=E4=BD=9C=E7=BA=BF=E7=A8=8B=E4=B8=AD?= =?UTF-8?q?=20[Mod]=20=E4=BF=AE=E6=94=B9=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=96=B9=E5=BC=8F=EF=BC=9AonError=E5=B0=86=E4=BC=9A?= =?UTF-8?q?=E6=8D=95=E6=8D=89=E6=9B=B4=E5=A4=9A=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit onError目前比以前能够多捕获两种异常: * websocket首次连接时的异常 * RestClient._createSession中产生的异常 --- vnpy/api/rest/RestClient.py | 32 +++++++++++------ vnpy/api/websocket/WebsocketClient.py | 51 +++++++++++++++------------ 2 files changed, 49 insertions(+), 34 deletions(-) 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):