[Mod] 将websocket._connect移动到工作线程中
[Mod] 修改错误处理方式:onError将会捕捉更多异常 onError目前比以前能够多捕获两种异常: * websocket首次连接时的异常 * RestClient._createSession中产生的异常
This commit is contained in:
parent
309a7e2c0a
commit
759489b1fd
@ -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)
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user