PhysicsOfMobility/ridepy

Allow to continue interrupted simulation runs

fxjung opened this issue · 0 comments

This makes sense for SimulationSet runs. The events are already serialized, the missing bit is dumping the current FleetState/Vehicle states/stoplists/current time/request generator state to disk before exiting. This should probably handle both keyboard interrupts and signals.

Example for keyboard interrupts:

import time

try:
    while True:
        time.sleep(1)
        print("doing something in a loop ...")
except KeyboardInterrupt:
    print('Exiting gracefully.... byebye')

Examples for signals:

import signal
import time

class GracefulKiller:
  kill_now = False
  def __init__(self):
    signal.signal(signal.SIGINT, self.exit_gracefully)
    signal.signal(signal.SIGTERM, self.exit_gracefully)
    signal.signal(signal.SIGQUIT, self.exit_gracefully)

  def exit_gracefully(self, signum, frame):
    self.kill_now = True

if __name__ == '__main__':
  killer = GracefulKiller()
  while not killer.kill_now:
    time.sleep(1)
    print("doing something in a loop ...")
   
  print("End of the program. I was killed gracefully :)")

(IPython sends KeyboardInterrupt on hitting the stop button.)

Crucially, it's likely not sufficient to just write pickles to disk. The exact step at which the simulation was interrupted needs to be found, discarding the incomplete step and storing the last state before the interruption. Also, a line in a file might be written partially, stoplists may not have been updated, etc.