ws - Improve cleanup behavior

This commit is contained in:
Matthias
2023-12-28 10:59:53 +01:00
parent 4e75e59476
commit d42e012ec3

View File

@@ -29,6 +29,7 @@ class ExchangeWS:
self.klines_last_request: Dict[PairWithTimeframe, float] = {}
self._thread = Thread(name="ccxt_ws", target=self._start_forever)
self._thread.start()
self.__cleanup_called = False
def _start_forever(self) -> None:
self._loop = asyncio.new_event_loop()
@@ -41,12 +42,22 @@ class ExchangeWS:
def cleanup(self) -> None:
logger.debug("Cleanup called - stopping")
self._klines_watching.clear()
for task in self._background_tasks:
task.cancel()
if hasattr(self, '_loop'):
asyncio.run_coroutine_threadsafe(self._cleanup_async(), loop=self._loop)
while not self.__cleanup_called:
time.sleep(0.1)
self._loop.call_soon_threadsafe(self._loop.stop)
self._thread.join()
logger.debug("Stopped")
async def _cleanup_async(self) -> None:
await self.ccxt_object.close()
self.__cleanup_called = True
def cleanup_expired(self) -> None:
"""
Remove pairs from watchlist if they've not been requested within