Loading...
Searching...
No Matches
interrupt.py
Go to the documentation of this file.
1
9
10import argparse
11import signal
12import sys
13import threading
14import time
15from gams import GamsWorkspace
16
17
18def interrupt_gams(job):
19 time.sleep(2)
20 job.interrupt()
21
22
23if __name__ == "__main__":
24 parser = argparse.ArgumentParser()
25 parser.add_argument("sysDir", nargs="?", default=None)
26 parser.add_argument("workDir", nargs="?", default=None)
27 parser.add_argument("-nonInteractive", action="store_true")
28 args = parser.parse_args()
29
30 ws = GamsWorkspace(system_directory=args.sysDir, working_directory=args.workDir)
31
32 # Use a model that needs some time to solve
33 ws.gamslib("dicex")
34 job = ws.add_job_from_file("dicex.gms")
35 opt = ws.add_options()
36 opt.all_model_types = "scip"
37
38 if args.nonInteractive:
39 # start thread asynchronously that interrupts the GamsJob after 2 seconds
40 threading.Thread(target=interrupt_gams, args=(job,)).start()
41 else:
42 # register signal to job.interrupt
43 signal.signal(signal.SIGINT, lambda signal, frame: job.interrupt())
44
45 # start GamsJob
46 job.run(opt, output=sys.stdout)