made a graceful sleep method
This commit is contained in:
@@ -40,6 +40,13 @@ class ADSubsystem:
|
||||
def stopping(self) -> bool:
|
||||
return self.stop.is_set()
|
||||
|
||||
async def sleep(self, delay: float):
|
||||
try:
|
||||
if not self.stopping:
|
||||
await asyncio.sleep(delay)
|
||||
except asyncio.CancelledError:
|
||||
self.logger.debug('Cancelled during sleep')
|
||||
|
||||
|
||||
@dataclass
|
||||
class Utility(ADSubsystem):
|
||||
@@ -53,10 +60,7 @@ class Utility(ADSubsystem):
|
||||
async def loop(self):
|
||||
while not self.stopping:
|
||||
self.logger.debug('Looping...')
|
||||
try:
|
||||
await asyncio.sleep(self.loop_rate)
|
||||
except asyncio.CancelledError:
|
||||
self.logger.debug('Cancelled during sleep')
|
||||
await self.sleep(self.loop_rate)
|
||||
self.logger.debug('Stopped utility loop gracefully')
|
||||
|
||||
|
||||
@@ -94,10 +98,8 @@ class Plugin(ADSubsystem):
|
||||
|
||||
# if self.state['update_count'] == 2:
|
||||
# raise ValueError('fake error')
|
||||
try:
|
||||
await asyncio.sleep(self.update_rate)
|
||||
except asyncio.CancelledError:
|
||||
self.logger.debug('Cancelled during sleep')
|
||||
|
||||
await self.sleep(self.update_rate)
|
||||
self.logger.debug('Stopped plugin updates gracefully')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user