improve shutdown time
on shutdown, time.sleep(10) is blocking the reconnect thread. change the _shutdown attribute from bool to an Event, and use Event.wait instead of time.sleep Change-Id: Icea6a14ad73df0b3d26ef45806f4c05e6bf18492
This commit is contained in:
parent
6c49abea74
commit
cb2c10655c
@ -261,7 +261,7 @@ class SecopClient(ProxyClient):
|
|||||||
"""a general SECoP client"""
|
"""a general SECoP client"""
|
||||||
reconnect_timeout = 10
|
reconnect_timeout = 10
|
||||||
_running = False
|
_running = False
|
||||||
_shutdown = False
|
_shutdown = None
|
||||||
_rxthread = None
|
_rxthread = None
|
||||||
_txthread = None
|
_txthread = None
|
||||||
_connthread = None
|
_connthread = None
|
||||||
@ -283,6 +283,7 @@ class SecopClient(ProxyClient):
|
|||||||
self.uri = uri
|
self.uri = uri
|
||||||
self.nodename = uri
|
self.nodename = uri
|
||||||
self._lock = RLock()
|
self._lock = RLock()
|
||||||
|
self._shutdown = Event()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
try:
|
try:
|
||||||
@ -303,7 +304,7 @@ class SecopClient(ProxyClient):
|
|||||||
else:
|
else:
|
||||||
self._set_state(False, 'connecting')
|
self._set_state(False, 'connecting')
|
||||||
deadline = time.time() + try_period
|
deadline = time.time() + try_period
|
||||||
while not self._shutdown:
|
while not self._shutdown.is_set():
|
||||||
try:
|
try:
|
||||||
self.io = AsynConn(self.uri) # timeout 1 sec
|
self.io = AsynConn(self.uri) # timeout 1 sec
|
||||||
self.io.writeline(IDENTREQUEST.encode('utf-8'))
|
self.io.writeline(IDENTREQUEST.encode('utf-8'))
|
||||||
@ -339,8 +340,8 @@ class SecopClient(ProxyClient):
|
|||||||
# stay online for now, if activated
|
# stay online for now, if activated
|
||||||
self._set_state(self.online and self.activate)
|
self._set_state(self.online and self.activate)
|
||||||
raise
|
raise
|
||||||
time.sleep(1)
|
self._shutdown.wait(1)
|
||||||
if not self._shutdown:
|
if not self._shutdown.is_set():
|
||||||
self.log.info('%s ready', self.nodename)
|
self.log.info('%s ready', self.nodename)
|
||||||
|
|
||||||
def __txthread(self):
|
def __txthread(self):
|
||||||
@ -436,7 +437,7 @@ class SecopClient(ProxyClient):
|
|||||||
self.log.error('rxthread ended with %r', e)
|
self.log.error('rxthread ended with %r', e)
|
||||||
self._rxthread = None
|
self._rxthread = None
|
||||||
self.disconnect(False)
|
self.disconnect(False)
|
||||||
if self._shutdown:
|
if self._shutdown.is_set():
|
||||||
return
|
return
|
||||||
if self.activate:
|
if self.activate:
|
||||||
self.log.info('try to reconnect to %s', self.uri)
|
self.log.info('try to reconnect to %s', self.uri)
|
||||||
@ -454,7 +455,7 @@ class SecopClient(ProxyClient):
|
|||||||
self._connthread = mkthread(self._reconnect, connected_callback)
|
self._connthread = mkthread(self._reconnect, connected_callback)
|
||||||
|
|
||||||
def _reconnect(self, connected_callback=None):
|
def _reconnect(self, connected_callback=None):
|
||||||
while not self._shutdown:
|
while not self._shutdown.is_set():
|
||||||
try:
|
try:
|
||||||
self.connect()
|
self.connect()
|
||||||
if connected_callback:
|
if connected_callback:
|
||||||
@ -474,15 +475,15 @@ class SecopClient(ProxyClient):
|
|||||||
self.log.info('continue trying to reconnect')
|
self.log.info('continue trying to reconnect')
|
||||||
# self.log.warning(formatExtendedTraceback())
|
# self.log.warning(formatExtendedTraceback())
|
||||||
self._set_state(False)
|
self._set_state(False)
|
||||||
time.sleep(self.reconnect_timeout)
|
self._shutdown.wait(self.reconnect_timeout)
|
||||||
else:
|
else:
|
||||||
time.sleep(1)
|
self._shutdown.wait(1)
|
||||||
self._connthread = None
|
self._connthread = None
|
||||||
|
|
||||||
def disconnect(self, shutdown=True):
|
def disconnect(self, shutdown=True):
|
||||||
self._running = False
|
self._running = False
|
||||||
if shutdown:
|
if shutdown:
|
||||||
self._shutdown = True
|
self._shutdown.set()
|
||||||
self._set_state(False, 'shutdown')
|
self._set_state(False, 'shutdown')
|
||||||
if self._connthread:
|
if self._connthread:
|
||||||
if self._connthread == current_thread():
|
if self._connthread == current_thread():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user