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