moved sleep function
This commit is contained in:
@@ -111,3 +111,10 @@ class AppDaemonRunContext:
|
|||||||
return await asyncio.wait_for(coro, timeout)
|
return await asyncio.wait_for(coro, timeout)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
print('Timed out')
|
print('Timed out')
|
||||||
|
|
||||||
|
async def sleep(self, delay: float):
|
||||||
|
"""Wrapper function for asyncio.sleep that suppresses and logs a task cancellation"""
|
||||||
|
try:
|
||||||
|
await self.run_in_executor(self.stop_event.wait, delay)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.debug('Cancelled during sleep')
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class ADSubsystem:
|
|||||||
name = f'_{self.__class__.__name__.lower()}'
|
name = f'_{self.__class__.__name__.lower()}'
|
||||||
self.logger = getLogger(f'AppDaemon.{name}')
|
self.logger = getLogger(f'AppDaemon.{name}')
|
||||||
self.create_task = self.AD.create_task
|
self.create_task = self.AD.create_task
|
||||||
|
self.sleep = self.AD.context.sleep
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.logger.debug(f'Starting {self.__class__.__name__}')
|
self.logger.debug(f'Starting {self.__class__.__name__}')
|
||||||
@@ -41,16 +42,6 @@ class ADSubsystem:
|
|||||||
def stopping(self) -> bool:
|
def stopping(self) -> bool:
|
||||||
return self.stop.is_set()
|
return self.stop.is_set()
|
||||||
|
|
||||||
async def sleep(self, delay: float):
|
|
||||||
"""Wrapper function for asyncio.sleep that suppresses and logs a task cancellation"""
|
|
||||||
try:
|
|
||||||
if not self.stopping:
|
|
||||||
await asyncio.sleep(delay)
|
|
||||||
else:
|
|
||||||
self.logger.debug('Skipping sleep due to stop event')
|
|
||||||
except asyncio.CancelledError:
|
|
||||||
self.logger.debug('Cancelled during sleep')
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Utility(ADSubsystem):
|
class Utility(ADSubsystem):
|
||||||
@@ -102,8 +93,8 @@ class Plugin(ADSubsystem):
|
|||||||
)
|
)
|
||||||
loop_time = perf_counter()
|
loop_time = perf_counter()
|
||||||
|
|
||||||
# if self.state['update_count'] == 2:
|
if self.state['update_count'] == 2:
|
||||||
# raise ValueError('fake error')
|
raise ValueError('fake error')
|
||||||
|
|
||||||
await self.sleep(self.update_rate)
|
await self.sleep(self.update_rate)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user