more context manager work
This commit is contained in:
@@ -36,9 +36,6 @@ class AppDaemonRunContext:
|
||||
for s in signals:
|
||||
self.loop.add_signal_handler(s, lambda s=s: self.loop.create_task(self.shutdown(s)))
|
||||
|
||||
# self.loop.add_signal_handler(signal.SIGINT, self.handle_signal, signal.SIGINT)
|
||||
# self.loop.add_signal_handler(signal.SIGTERM, self.handle_signal)
|
||||
# self.executor = self._stack.enter_context(ThreadPoolExecutor(max_workers=5))
|
||||
self.executor = self._stack.enter_context(self.thread_context())
|
||||
logger.debug("Entered threadpool context")
|
||||
return self
|
||||
@@ -56,22 +53,15 @@ class AppDaemonRunContext:
|
||||
if not self.stop_event.is_set():
|
||||
logger.debug('Setting stop event')
|
||||
self.stop_event.set()
|
||||
|
||||
tasks = [
|
||||
t for t in asyncio.all_tasks()
|
||||
|
||||
graceful = (
|
||||
asyncio.wait_for(t, timeout=2.0)
|
||||
for t in asyncio.all_tasks()
|
||||
if t is not asyncio.current_task()
|
||||
]
|
||||
)
|
||||
logger.debug('Allowing graceful shutdown from stop event')
|
||||
await asyncio.gather(*graceful, return_exceptions=True)
|
||||
|
||||
for task in tasks:
|
||||
# logger.debug(f"Waiting on task to finish: {task.get_coro().__qualname__}")
|
||||
logger.debug(f"Cancelling {task.get_name()}: {task.get_coro().__qualname__}")
|
||||
task.cancel()
|
||||
|
||||
logger.debug('Waiting for tasks to finish...')
|
||||
try:
|
||||
await asyncio.gather(*tasks, return_exceptions=True)
|
||||
except asyncio.CancelledError as e:
|
||||
logger.debug(f'Cancelled: {e}')
|
||||
logger.debug("Stopping event loop in context shutdown")
|
||||
self.loop.stop()
|
||||
else:
|
||||
|
||||
@@ -46,7 +46,7 @@ class Utility(ADSubsystem):
|
||||
@dataclass
|
||||
class Plugin(ADSubsystem):
|
||||
state: dict[str, int] = field(default_factory=dict)
|
||||
update_rate: float = 10.0
|
||||
update_rate: float = 30.0
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
super().__post_init__()
|
||||
@@ -115,7 +115,7 @@ if __name__ == '__main__':
|
||||
{
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
'formatters': {'basic': {'style': '{', 'format': '{message}'}},
|
||||
'formatters': {'basic': {'style': '{', 'format': '[yellow]{name}[/] {message}'}},
|
||||
'handlers': {
|
||||
'rich': {
|
||||
'()': 'rich.logging.RichHandler',
|
||||
|
||||
Reference in New Issue
Block a user