daemon: change stop() to use events, instead of polling
This commit is contained in:
@@ -413,8 +413,6 @@ class Daemon(Logger):
|
|||||||
@profiler
|
@profiler
|
||||||
def __init__(self, config: SimpleConfig, fd=None, *, listen_jsonrpc=True):
|
def __init__(self, config: SimpleConfig, fd=None, *, listen_jsonrpc=True):
|
||||||
Logger.__init__(self)
|
Logger.__init__(self)
|
||||||
self.running = False
|
|
||||||
self.running_lock = threading.Lock()
|
|
||||||
self.config = config
|
self.config = config
|
||||||
if fd is None and listen_jsonrpc:
|
if fd is None and listen_jsonrpc:
|
||||||
fd = get_file_descriptor(config)
|
fd = get_file_descriptor(config)
|
||||||
@@ -455,6 +453,7 @@ class Daemon(Logger):
|
|||||||
if self.config.get('use_gossip', False):
|
if self.config.get('use_gossip', False):
|
||||||
self.network.start_gossip()
|
self.network.start_gossip()
|
||||||
|
|
||||||
|
self.stopping_soon = threading.Event()
|
||||||
self.stopped_event = asyncio.Event()
|
self.stopped_event = asyncio.Event()
|
||||||
self.taskgroup = TaskGroup()
|
self.taskgroup = TaskGroup()
|
||||||
asyncio.run_coroutine_threadsafe(self._run(jobs=daemon_jobs), self.asyncio_loop)
|
asyncio.run_coroutine_threadsafe(self._run(jobs=daemon_jobs), self.asyncio_loop)
|
||||||
@@ -474,6 +473,7 @@ class Daemon(Logger):
|
|||||||
self.logger.exception("taskgroup died.")
|
self.logger.exception("taskgroup died.")
|
||||||
finally:
|
finally:
|
||||||
self.logger.info("taskgroup stopped.")
|
self.logger.info("taskgroup stopped.")
|
||||||
|
self.stopping_soon.set()
|
||||||
|
|
||||||
def load_wallet(self, path, password, *, manual_upgrades=True) -> Optional[Abstract_Wallet]:
|
def load_wallet(self, path, password, *, manual_upgrades=True) -> Optional[Abstract_Wallet]:
|
||||||
path = standardize_path(path)
|
path = standardize_path(path)
|
||||||
@@ -531,21 +531,14 @@ class Daemon(Logger):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def run_daemon(self):
|
def run_daemon(self):
|
||||||
self.running = True
|
|
||||||
try:
|
try:
|
||||||
while self.is_running():
|
self.stopping_soon.wait()
|
||||||
time.sleep(0.1)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.running = False
|
self.stopping_soon.set()
|
||||||
self.on_stop()
|
self.on_stop()
|
||||||
|
|
||||||
def is_running(self):
|
|
||||||
with self.running_lock:
|
|
||||||
return self.running and not self.taskgroup.closed()
|
|
||||||
|
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
with self.running_lock:
|
self.stopping_soon.set()
|
||||||
self.running = False
|
|
||||||
await self.stopped_event.wait()
|
await self.stopped_event.wait()
|
||||||
|
|
||||||
def on_stop(self):
|
def on_stop(self):
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class Listener(util.DaemonThread):
|
|||||||
self.received.remove(keyhash)
|
self.received.remove(keyhash)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.running:
|
while self.is_running():
|
||||||
if not self.keyhashes:
|
if not self.keyhashes:
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user