ampl / amplpy

Python API for AMPL
https://amplpy.ampl.com
BSD 3-Clause "New" or "Revised" License
66 stars 19 forks source link

Optimization process will not be terminated when interrupted #26

Closed ytakashina closed 1 year ago

ytakashina commented 5 years ago

When I interrupted my python script which was running AMPL.solve(), ampl.exe and ampl_gurobi (solver) remain running whereas the original python script stopped. I think it's great if amplpy automatically kills these processes when interrupted.

I am using amplpy==0.6.7 on Windows 10.

fdabrandao commented 5 years ago

Thank you for reporting this issue. In the current version we do not define any signal handler to call ampl.close() when some termination signal is received since this method is called automatically when the object is deleted (which, however, may not always happen depending on the signal received). How are you interrupting the python script?

As a workaround you can define the signal handler yourself as follows:

from amplpy import AMPL
import signal

def signal_handler(signal_, frame):
    print("signal received: {0}".format(signal_))
    global ampl
    ampl.close()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)

ampl = AMPL()
ampl.read('gk_f10_n1500_m50.mod')
ampl.option['gurobi_options'] = 'outlev=1'
ampl.option['solver'] = 'gurobi'
ampl.solve()

You may need to use other signals depending on how you are interrupting the script.