diff --git a/lib/util.py b/lib/util.py index 7cbae72..4559cfd 100644 --- a/lib/util.py +++ b/lib/util.py @@ -133,12 +133,3 @@ def increment_byte_string(bs): # This can only happen if all characters are 0xff bs = bytes([1]) + bs return bytes(bs) - -async def asyncio_clean_shutdown(loop=None): - while True: - pending_tasks = [task for task in asyncio.Task.all_tasks(loop) - if not task.done()] - if len(pending_tasks) <= 1: - break - await asyncio.sleep(0) - await asyncio.sleep(1) diff --git a/server/mempool.py b/server/mempool.py index 81490f4..7cdf7f2 100644 --- a/server/mempool.py +++ b/server/mempool.py @@ -133,9 +133,10 @@ class MemPool(util.LoggedClass): loop = asyncio.get_event_loop() pending = [] txs = self.txs + first = True async def process(unprocessed): - nonlocal pending + nonlocal first, pending raw_txs = {} while unprocessed and len(raw_txs) < limit: @@ -163,9 +164,12 @@ class MemPool(util.LoggedClass): hash168s[hash168].add(hex_hash) to_do = len(unfetched) + len(unprocessed) - if to_do: - percent = (len(txs) - to_do) * 100 // len(txs) + if to_do and txs: + percent = max(0, len(txs) - to_do) * 100 // len(txs) self.logger.info('catchup {:d}% complete'.format(percent)) + elif first: + first = False + self.logger.info('caught up') return process @@ -257,7 +261,7 @@ class MemPool(util.LoggedClass): return [] hex_hashes = self.hash168s[hash168] - raw_txs = self.daemon.getrawtransactions(hex_hashes) + raw_txs = await self.daemon.getrawtransactions(hex_hashes) result = [] for hex_hash, raw_tx in zip(hex_hashes, raw_txs): item = self.txs.get(hex_hash) diff --git a/server/protocol.py b/server/protocol.py index 6ac272c..12aa04f 100644 --- a/server/protocol.py +++ b/server/protocol.py @@ -183,7 +183,7 @@ class ServerManager(util.LoggedClass): except asyncio.CancelledError: break await self.shutdown() - await util.asyncio_clean_shutdown() + await asyncio.sleep(1) async def start_server(self, kind, *args, **kw_args): protocol_class = LocalRPC if kind == 'RPC' else ElectrumX