From 55e23ed5c167d974fc7a6247ded232a97c32ba2c Mon Sep 17 00:00:00 2001 From: vhu43 <67619615+vhu43@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:04:50 -0700 Subject: [PATCH] Pacing loop --- evolver/evolver.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/evolver/evolver.py b/evolver/evolver.py index 7f130b0..1dc050c 100644 --- a/evolver/evolver.py +++ b/evolver/evolver.py @@ -1,4 +1,4 @@ -#!/usr/local/bin/env python3.6 +#!/usr/bin/env python import yaml import time import asyncio @@ -22,7 +22,7 @@ def start_background_loop(loop): evolver_ip = s.getsockname()[0] s.close() with open(os.path.realpath(os.path.join(os.getcwd(),os.path.dirname(__file__), CONF_FILENAME)), 'r') as ymlfile: - conf = yaml.load(ymlfile) + conf = yaml.safe_load(ymlfile) conf['evolver_ip'] = evolver_ip @@ -35,14 +35,17 @@ def start_background_loop(loop): # Set up data broadcasting bloop = asyncio.new_event_loop() - last_time = None + last_time = 0 running = False while True: current_time = time.time() + duration = current_time - last_time + commands_in_queue = evolver_server.get_num_commands() > 0 + time_reached = duration > conf['broadcast_timing'] - if (last_time is None or current_time - last_time > conf['broadcast_timing'] or commands_in_queue) and not running: - if last_time is None or current_time - last_time > conf['broadcast_timing']: + if (time_reached or commands_in_queue) and not running: + if time_reached: last_time = current_time try: running = True @@ -50,3 +53,4 @@ def start_background_loop(loop): running = False except: pass + time.sleep(0.1)