My goal is to run a program on different computers and get data from measurements. Problem is that those measurements need to happen at the same time (as much as possible). My current approach is to install crony to synchronize the time of all machines and start a python program that will take measurements at the start of each minute. This proved to be quite unwieldy as I have to poll the time.time() in a loop and check if we entered a new minute.
time_factor = 1 # 1000 if desired resolution is ms, 1 if seconds
interval_s = 60 * time_factor
old_interval = int(time.time()*time_factor) / interval_s
current_interval = None
running = True
while running:
t_s = int(time.time() * time_factor)
current_interval = t_s / interval_s
if (t_s % interval_s == 0) and (current_interval != old_interval):
request_runtime = time.time()
await self.do_time_sensitive_work()
old_interval = current_interval
request_runtime = time.time() - request_runtime
await asyncio.sleep(int(interval_s - request_runtime - 1))
else:
await asyncio.sleep(0.01)
Is there a standard solution for this type of a problem, or at least a more elegant solution?