aegis_sim.recording.ticker
1from multiprocessing import Process 2import time 3import pathlib 4from datetime import datetime 5from .recorder import Recorder 6import logging 7 8 9class Ticker(Recorder): 10 def __init__(self, TICKER_RATE, odir: pathlib.Path): 11 self.TICKER_RATE = TICKER_RATE 12 self.ticker_path = odir / "ticker.txt" 13 self.process = None 14 self.pid = None 15 16 def start_process(self): 17 self.process = Process(target=self.tick) 18 self.process.start() 19 self.pid = self.process.pid 20 # TODO check if this continues when simulation is interrupted or terminated otherwise 21 22 def stop_process(self): 23 self.process.terminate() 24 self.process.join() 25 26 def tick(self): 27 while True: 28 self.write() 29 time.sleep(self.TICKER_RATE) 30 31 def write(self): 32 """ 33 # OUTPUT SPECIFICATION 34 path: /ticker.txt 35 filetype: txt 36 category: log 37 description: A live file useful for determining whether the simulation is still running. It gets updated every TICKER_RATE seconds; if it is not updated, the simulation is not running. 38 trait granularity: N/A 39 time granularity: N/A 40 frequency parameter: TICKER_RATE 41 structure: A txt file with datetime stamp (%Y-%m-%d %H:%M:%S) in one line) 42 """ 43 timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 44 with open(self.ticker_path, "w") as file: 45 file.write(timestamp) 46 47 def read(self): 48 if not self.ticker_path.exists(): 49 logging.error(f"{self.ticker_path} does not exist.") 50 return 51 with open(self.ticker_path, "r") as file: 52 return file.read() 53 54 def has_stopped(self): 55 since_last = self.since_last() 56 return since_last > self.TICKER_RATE 57 58 def since_last(self): 59 timestamp_recorded = self.read() 60 if timestamp_recorded is None or timestamp_recorded == "": 61 logging.info(f"timestamp_recorded is '{timestamp_recorded}'") 62 return 63 timestamp_now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 64 dt_recorded = datetime.strptime(timestamp_recorded, "%Y-%m-%d %H:%M:%S") 65 dt_now = datetime.strptime(timestamp_now, "%Y-%m-%d %H:%M:%S") 66 time_difference = (dt_now - dt_recorded).total_seconds() 67 return time_difference
10class Ticker(Recorder): 11 def __init__(self, TICKER_RATE, odir: pathlib.Path): 12 self.TICKER_RATE = TICKER_RATE 13 self.ticker_path = odir / "ticker.txt" 14 self.process = None 15 self.pid = None 16 17 def start_process(self): 18 self.process = Process(target=self.tick) 19 self.process.start() 20 self.pid = self.process.pid 21 # TODO check if this continues when simulation is interrupted or terminated otherwise 22 23 def stop_process(self): 24 self.process.terminate() 25 self.process.join() 26 27 def tick(self): 28 while True: 29 self.write() 30 time.sleep(self.TICKER_RATE) 31 32 def write(self): 33 """ 34 # OUTPUT SPECIFICATION 35 path: /ticker.txt 36 filetype: txt 37 category: log 38 description: A live file useful for determining whether the simulation is still running. It gets updated every TICKER_RATE seconds; if it is not updated, the simulation is not running. 39 trait granularity: N/A 40 time granularity: N/A 41 frequency parameter: TICKER_RATE 42 structure: A txt file with datetime stamp (%Y-%m-%d %H:%M:%S) in one line) 43 """ 44 timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 45 with open(self.ticker_path, "w") as file: 46 file.write(timestamp) 47 48 def read(self): 49 if not self.ticker_path.exists(): 50 logging.error(f"{self.ticker_path} does not exist.") 51 return 52 with open(self.ticker_path, "r") as file: 53 return file.read() 54 55 def has_stopped(self): 56 since_last = self.since_last() 57 return since_last > self.TICKER_RATE 58 59 def since_last(self): 60 timestamp_recorded = self.read() 61 if timestamp_recorded is None or timestamp_recorded == "": 62 logging.info(f"timestamp_recorded is '{timestamp_recorded}'") 63 return 64 timestamp_now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 65 dt_recorded = datetime.strptime(timestamp_recorded, "%Y-%m-%d %H:%M:%S") 66 dt_now = datetime.strptime(timestamp_now, "%Y-%m-%d %H:%M:%S") 67 time_difference = (dt_now - dt_recorded).total_seconds() 68 return time_difference
def
write(self):
32 def write(self): 33 """ 34 # OUTPUT SPECIFICATION 35 path: /ticker.txt 36 filetype: txt 37 category: log 38 description: A live file useful for determining whether the simulation is still running. It gets updated every TICKER_RATE seconds; if it is not updated, the simulation is not running. 39 trait granularity: N/A 40 time granularity: N/A 41 frequency parameter: TICKER_RATE 42 structure: A txt file with datetime stamp (%Y-%m-%d %H:%M:%S) in one line) 43 """ 44 timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 45 with open(self.ticker_path, "w") as file: 46 file.write(timestamp)
OUTPUT SPECIFICATION
path: /ticker.txt filetype: txt category: log description: A live file useful for determining whether the simulation is still running. It gets updated every TICKER_RATE seconds; if it is not updated, the simulation is not running. trait granularity: N/A time granularity: N/A frequency parameter: TICKER_RATE structure: A txt file with datetime stamp (%Y-%m-%d %H:%M:%S) in one line)
def
since_last(self):
59 def since_last(self): 60 timestamp_recorded = self.read() 61 if timestamp_recorded is None or timestamp_recorded == "": 62 logging.info(f"timestamp_recorded is '{timestamp_recorded}'") 63 return 64 timestamp_now = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) 65 dt_recorded = datetime.strptime(timestamp_recorded, "%Y-%m-%d %H:%M:%S") 66 dt_now = datetime.strptime(timestamp_now, "%Y-%m-%d %H:%M:%S") 67 time_difference = (dt_now - dt_recorded).total_seconds() 68 return time_difference