more context manager work
This commit is contained in:
@@ -36,9 +36,6 @@ class AppDaemonRunContext:
|
|||||||
for s in signals:
|
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(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())
|
self.executor = self._stack.enter_context(self.thread_context())
|
||||||
logger.debug("Entered threadpool context")
|
logger.debug("Entered threadpool context")
|
||||||
return self
|
return self
|
||||||
@@ -57,21 +54,14 @@ class AppDaemonRunContext:
|
|||||||
logger.debug('Setting stop event')
|
logger.debug('Setting stop event')
|
||||||
self.stop_event.set()
|
self.stop_event.set()
|
||||||
|
|
||||||
tasks = [
|
graceful = (
|
||||||
t for t in asyncio.all_tasks()
|
asyncio.wait_for(t, timeout=2.0)
|
||||||
|
for t in asyncio.all_tasks()
|
||||||
if t is not asyncio.current_task()
|
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")
|
logger.debug("Stopping event loop in context shutdown")
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class Utility(ADSubsystem):
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Plugin(ADSubsystem):
|
class Plugin(ADSubsystem):
|
||||||
state: dict[str, int] = field(default_factory=dict)
|
state: dict[str, int] = field(default_factory=dict)
|
||||||
update_rate: float = 10.0
|
update_rate: float = 30.0
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
@@ -115,7 +115,7 @@ if __name__ == '__main__':
|
|||||||
{
|
{
|
||||||
'version': 1,
|
'version': 1,
|
||||||
'disable_existing_loggers': False,
|
'disable_existing_loggers': False,
|
||||||
'formatters': {'basic': {'style': '{', 'format': '{message}'}},
|
'formatters': {'basic': {'style': '{', 'format': '[yellow]{name}[/] {message}'}},
|
||||||
'handlers': {
|
'handlers': {
|
||||||
'rich': {
|
'rich': {
|
||||||
'()': 'rich.logging.RichHandler',
|
'()': 'rich.logging.RichHandler',
|
||||||
|
|||||||
Reference in New Issue
Block a user