Use threading event in main loop.

2024-05-20_merge
tecnovert 1 year ago
parent 20b405a944
commit e7ae290eb5
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
  1. 5
      basicswap/base.py
  2. 11
      basicswap/basicswap.py
  3. 6
      bin/basicswap_run.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):

@ -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)

@ -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:

Loading…
Cancel
Save