Skip to content

Commit

Permalink
adding a Ctrl-C interrupt handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown committed May 8, 2019
1 parent cd7df2b commit 859fad9
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/projects/secondary_voltage_control/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import argparse
from src.core.parse_project_configuration import *
from src.core.net_power import *
import signal
import sys

Interrupted = False
def signal_handler(sig, frame):
global Interrupted
print('You pressed Ctrl+C! Scheduling smooth exit!')
Interrupted = True



def main():

global Interrupted
parser = argparse.ArgumentParser()
parser.add_argument('--run_time', dest="run_time", type=int, default=10,
help="Running time secs for the experiment (in virtual/real time)")
Expand All @@ -15,6 +24,7 @@ def main():
help="Relative cpu speed of processes if kronos is enabled")

args = parser.parse_args()
signal.signal(signal.SIGINT, signal_handler)

# project_dir is the directory in which this script is located
project_dir = os.path.dirname(os.path.realpath(__file__))
Expand All @@ -30,24 +40,27 @@ def main():
exp = parse_experiment_configuration(project_run_time_args)
exp.initialize_project()


total_time_ran = 0

#MS, SEC are defined in src/core/defines.py
timestep_size = 10*MS


# Main Loop of Co-Simulation
while True:
while Interrupted == False:

if total_time_ran == 1*SEC:
print "Triggering nxt replay pcap ..."
exp.trigger_nxt_replay()

exp.run_for(timestep_size)
total_time_ran += timestep_size

print "Time Elapsed (Secs): ", float(total_time_ran)/float(SEC)
if total_time_ran >= args.run_time*SEC:
if args.enable_kronos == 1:
print "Virtual Time Elapsed (Secs): ", float(total_time_ran)/float(SEC)
else:
print "Time Elapsed (Secs): ", float(total_time_ran)/float(SEC)
if total_time_ran >= args.run_time*SEC or Interrupted == True:
break

exp.close_project()
Expand Down

0 comments on commit 859fad9

Please sign in to comment.