I wrote a simulation involving two scripts running in different consoles. The scripts are sending messages to each other via websocket. Messages are sent in defined intervals and contain a timestamp (currently I use datetime.utcnow
). Now when I speed up the simulation,'naturally the datetime
timestamps are unaffected by this which means they are out of sync with the simulation time. Is there a way to "speed up" the system time or do i have to write my own timestamp function where i can determine the speed?
Edit: Since i can't change the system time I wrote a script with a clock that runs at a speed I can determine and which can generate timestamps. However I have can't find a way to run this clock in the backgroud without blocking the console. I thought i could use asyncio but it is not working like i expected. This is the code:
import asyncio
import os
import math
simtime = 0
year = 2020
month = 10
try:
TIMELAPSE = int(os.environ["TIMELAPSE"])
except KeyError:
TIMELAPSE = 1
async def clock():
global simtime
while True:
await asyncio.sleep(1)
simtime += 1*TIMELAPSE
def timestamp():
day = 1 + math.floor(simtime/86400)
remaining = simtime % 86400
hours = math.floor(remaining/3600)
remaining = remaining % 3600
minutes = math.floor(remaining/60)
seconds = remaining % 60
return f"{year}-{month}-{day}::{hours}:{minutes}:{seconds}"
loop = asyncio.get_event_loop()
loop.run_until_complete(clock())
loop.run_forever()
I thought the time would proceed in the background i can get the current time with the timestamp() function.
However, when i do import clock
, it blocks the script