chore: improve shutdown for exchange_ws

This commit is contained in:
Matthias
2026-04-23 20:30:31 +02:00
parent 2e4b92ea6c
commit a625cbecff
+7 -7
View File
@@ -31,7 +31,6 @@ class ExchangeWS:
self.klines_last_request: dict[PairWithTimeframe, float] = {} self.klines_last_request: dict[PairWithTimeframe, float] = {}
self._thread = Thread(name="ccxt_ws", target=self._start_forever) self._thread = Thread(name="ccxt_ws", target=self._start_forever)
self._thread.start() self._thread.start()
self.__cleanup_called = False
def _start_forever(self) -> None: def _start_forever(self) -> None:
self._loop = asyncio.new_event_loop() self._loop = asyncio.new_event_loop()
@@ -63,10 +62,13 @@ class ExchangeWS:
""" """
if hasattr(self, "_loop") and not self._loop.is_closed(): if hasattr(self, "_loop") and not self._loop.is_closed():
logger.info("Resetting WS connections.") logger.info("Resetting WS connections.")
asyncio.run_coroutine_threadsafe(self._cleanup_async(), loop=self._loop) try:
while not self.__cleanup_called: fut = asyncio.run_coroutine_threadsafe(self._cleanup_async(), loop=self._loop)
time.sleep(0.1) fut.result(timeout=10)
self.__cleanup_called = False except TimeoutError:
logger.warning("Timed out while resetting websocket connections.")
except Exception:
logger.exception("Exception while resetting websocket connections")
async def _cleanup_async(self) -> None: async def _cleanup_async(self) -> None:
try: try:
@@ -76,8 +78,6 @@ class ExchangeWS:
self._ccxt_object.ohlcvs.clear() self._ccxt_object.ohlcvs.clear()
except Exception: except Exception:
logger.exception("Exception in _cleanup_async") logger.exception("Exception in _cleanup_async")
finally:
self.__cleanup_called = True
def _pop_history(self, paircomb: PairWithTimeframe) -> None: def _pop_history(self, paircomb: PairWithTimeframe) -> None:
""" """