From e7ae290eb5fbb8ebd39e289f0d388739b8c14a47 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Sun, 17 Sep 2023 22:34:48 +0200 Subject: [PATCH] Use threading event in main loop. --- basicswap/base.py | 5 +++-- basicswap/basicswap.py | 11 +++++------ bin/basicswap_run.py | 6 ++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/basicswap/base.py b/basicswap/base.py index 4f10904..b947d28 100644 --- a/basicswap/base.py +++ b/basicswap/base.py @@ -37,7 +37,6 @@ class BaseApp: def __init__(self, fp, data_dir, settings, chain, log_name='BasicSwap'): self.log_name = log_name self.fp = fp - self.is_running = True self.fail_code = 0 self.mock_time_offset = 0 @@ -49,6 +48,8 @@ class BaseApp: self.mxDB = threading.RLock() self.debug = self.settings.get('debug', False) self.delay_event = threading.Event() + self.chainstate_delay_event = threading.Event() + self._network = None self.prepareLogging() self.log.info('Network: {}'.format(self.chain)) @@ -65,7 +66,7 @@ class BaseApp: def stopRunning(self, with_code=0): self.fail_code = with_code with self.mxDB: - self.is_running = False + self.chainstate_delay_event.set() self.delay_event.set() def prepareLogging(self): diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index de81cbe..6a66914 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -164,7 +164,7 @@ def validOfferStateToReceiveBid(offer_state): def threadPollXMRChainState(swap_client, coin_type): ci = swap_client.ci(coin_type) cc = swap_client.coin_clients[coin_type] - while not swap_client.delay_event.is_set(): + while not swap_client.chainstate_delay_event.is_set(): try: new_height = ci.getChainHeight() if new_height != cc['chain_height']: @@ -173,13 +173,13 @@ def threadPollXMRChainState(swap_client, coin_type): cc['chain_height'] = new_height except Exception as e: swap_client.log.warning('threadPollXMRChainState {}, error: {}'.format(ci.ticker(), str(e))) - swap_client.delay_event.wait(random.randrange(20, 30)) # random to stagger updates + swap_client.chainstate_delay_event.wait(random.randrange(20, 30)) # random to stagger updates def threadPollChainState(swap_client, coin_type): ci = swap_client.ci(coin_type) cc = swap_client.coin_clients[coin_type] - while not swap_client.delay_event.is_set(): + while not swap_client.chainstate_delay_event.is_set(): try: chain_state = ci.getBlockchainInfo() if chain_state['bestblockhash'] != cc['chain_best_block']: @@ -191,7 +191,7 @@ def threadPollChainState(swap_client, coin_type): cc['chain_median_time'] = chain_state['mediantime'] except Exception as e: swap_client.log.warning('threadPollChainState {}, error: {}'.format(ci.ticker(), str(e))) - swap_client.delay_event.wait(random.randrange(20, 30)) # random to stagger updates + swap_client.chainstate_delay_event.wait(random.randrange(20, 30)) # random to stagger updates class WatchedOutput(): # Watch for spends @@ -372,7 +372,6 @@ class BasicSwap(BaseApp): self.log.info('Finalise') with self.mxDB: - self.is_running = False self.delay_event.set() if self._network: @@ -796,7 +795,7 @@ class BasicSwap(BaseApp): if 'startup_tries' in chain_client_settings: startup_tries = chain_client_settings['startup_tries'] for i in range(startup_tries): - if not self.is_running: + if self.delay_event.is_set(): return try: self.coin_clients[coin_type]['interface'].testDaemonRPC(with_wallet) diff --git a/bin/basicswap_run.py b/bin/basicswap_run.py index 625916e..bc56074 100755 --- a/bin/basicswap_run.py +++ b/bin/basicswap_run.py @@ -1,14 +1,13 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright (c) 2019-2022 tecnovert +# Copyright (c) 2019-2023 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. import os import sys import json -import time import shutil import signal import logging @@ -210,8 +209,7 @@ def runClient(fp, data_dir, chain): swap_client.ws_server.run_forever(threaded=True) logger.info('Exit with Ctrl + c.') - while swap_client.is_running: - time.sleep(0.5) + while not swap_client.delay_event.wait(0.5): swap_client.update() except Exception as ex: