mirror of
https://github.com/basicswap/basicswap.git
synced 2024-12-23 03:49:25 +00:00
Use threading event in main loop.
This commit is contained in:
parent
20b405a944
commit
e7ae290eb5
3 changed files with 10 additions and 12 deletions
|
@ -37,7 +37,6 @@ class BaseApp:
|
||||||
def __init__(self, fp, data_dir, settings, chain, log_name='BasicSwap'):
|
def __init__(self, fp, data_dir, settings, chain, log_name='BasicSwap'):
|
||||||
self.log_name = log_name
|
self.log_name = log_name
|
||||||
self.fp = fp
|
self.fp = fp
|
||||||
self.is_running = True
|
|
||||||
self.fail_code = 0
|
self.fail_code = 0
|
||||||
self.mock_time_offset = 0
|
self.mock_time_offset = 0
|
||||||
|
|
||||||
|
@ -49,6 +48,8 @@ class BaseApp:
|
||||||
self.mxDB = threading.RLock()
|
self.mxDB = threading.RLock()
|
||||||
self.debug = self.settings.get('debug', False)
|
self.debug = self.settings.get('debug', False)
|
||||||
self.delay_event = threading.Event()
|
self.delay_event = threading.Event()
|
||||||
|
self.chainstate_delay_event = threading.Event()
|
||||||
|
|
||||||
self._network = None
|
self._network = None
|
||||||
self.prepareLogging()
|
self.prepareLogging()
|
||||||
self.log.info('Network: {}'.format(self.chain))
|
self.log.info('Network: {}'.format(self.chain))
|
||||||
|
@ -65,7 +66,7 @@ class BaseApp:
|
||||||
def stopRunning(self, with_code=0):
|
def stopRunning(self, with_code=0):
|
||||||
self.fail_code = with_code
|
self.fail_code = with_code
|
||||||
with self.mxDB:
|
with self.mxDB:
|
||||||
self.is_running = False
|
self.chainstate_delay_event.set()
|
||||||
self.delay_event.set()
|
self.delay_event.set()
|
||||||
|
|
||||||
def prepareLogging(self):
|
def prepareLogging(self):
|
||||||
|
|
|
@ -164,7 +164,7 @@ def validOfferStateToReceiveBid(offer_state):
|
||||||
def threadPollXMRChainState(swap_client, coin_type):
|
def threadPollXMRChainState(swap_client, coin_type):
|
||||||
ci = swap_client.ci(coin_type)
|
ci = swap_client.ci(coin_type)
|
||||||
cc = swap_client.coin_clients[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:
|
try:
|
||||||
new_height = ci.getChainHeight()
|
new_height = ci.getChainHeight()
|
||||||
if new_height != cc['chain_height']:
|
if new_height != cc['chain_height']:
|
||||||
|
@ -173,13 +173,13 @@ def threadPollXMRChainState(swap_client, coin_type):
|
||||||
cc['chain_height'] = new_height
|
cc['chain_height'] = new_height
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
swap_client.log.warning('threadPollXMRChainState {}, error: {}'.format(ci.ticker(), str(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):
|
def threadPollChainState(swap_client, coin_type):
|
||||||
ci = swap_client.ci(coin_type)
|
ci = swap_client.ci(coin_type)
|
||||||
cc = swap_client.coin_clients[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:
|
try:
|
||||||
chain_state = ci.getBlockchainInfo()
|
chain_state = ci.getBlockchainInfo()
|
||||||
if chain_state['bestblockhash'] != cc['chain_best_block']:
|
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']
|
cc['chain_median_time'] = chain_state['mediantime']
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
swap_client.log.warning('threadPollChainState {}, error: {}'.format(ci.ticker(), str(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
|
class WatchedOutput(): # Watch for spends
|
||||||
|
@ -372,7 +372,6 @@ class BasicSwap(BaseApp):
|
||||||
self.log.info('Finalise')
|
self.log.info('Finalise')
|
||||||
|
|
||||||
with self.mxDB:
|
with self.mxDB:
|
||||||
self.is_running = False
|
|
||||||
self.delay_event.set()
|
self.delay_event.set()
|
||||||
|
|
||||||
if self._network:
|
if self._network:
|
||||||
|
@ -796,7 +795,7 @@ class BasicSwap(BaseApp):
|
||||||
if 'startup_tries' in chain_client_settings:
|
if 'startup_tries' in chain_client_settings:
|
||||||
startup_tries = chain_client_settings['startup_tries']
|
startup_tries = chain_client_settings['startup_tries']
|
||||||
for i in range(startup_tries):
|
for i in range(startup_tries):
|
||||||
if not self.is_running:
|
if self.delay_event.is_set():
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self.coin_clients[coin_type]['interface'].testDaemonRPC(with_wallet)
|
self.coin_clients[coin_type]['interface'].testDaemonRPC(with_wallet)
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Copyright (c) 2019-2022 tecnovert
|
# Copyright (c) 2019-2023 tecnovert
|
||||||
# Distributed under the MIT software license, see the accompanying
|
# Distributed under the MIT software license, see the accompanying
|
||||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
import shutil
|
import shutil
|
||||||
import signal
|
import signal
|
||||||
import logging
|
import logging
|
||||||
|
@ -210,8 +209,7 @@ def runClient(fp, data_dir, chain):
|
||||||
swap_client.ws_server.run_forever(threaded=True)
|
swap_client.ws_server.run_forever(threaded=True)
|
||||||
|
|
||||||
logger.info('Exit with Ctrl + c.')
|
logger.info('Exit with Ctrl + c.')
|
||||||
while swap_client.is_running:
|
while not swap_client.delay_event.wait(0.5):
|
||||||
time.sleep(0.5)
|
|
||||||
swap_client.update()
|
swap_client.update()
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
|
Loading…
Reference in a new issue