Adjust sleep values based on execution time for python south service
@@ -58,11 +58,14 @@ class Server(FoglampMicroservice):
_task_main = None
+ _event_loop = None
+
def __init__(self):
super().__init__()
async def _start(self, loop) -> None:
error = None
+ self._event_loop = loop
try:
# Configuration handling - initial configuration
category = self._name
@@ -172,6 +175,7 @@ class Server(FoglampMicroservice):
while self._plugin and try_count <= _MAX_RETRY_POLL:
try:
+ t1 = self._event_loop.time()
data = self._plugin.plugin_poll(self._plugin_handle)
if len(data) > 0:
if isinstance(data, list):
@@ -185,7 +189,9 @@ class Server(FoglampMicroservice):
timestamp=data['timestamp'],
key=data['key'],
readings=data['readings']))
- await asyncio.sleep(sleep_seconds)
+ delta = self._event_loop.time() - t1
+ # If delta somehow becomes > sleep_seconds, then do not sleep
+ await asyncio.sleep(sleep_seconds - delta)
except asyncio.CancelledError:
pass
except KeyError as ex:
