Stopping Gurobipy on exception
回答済みI'm building a Python API where with an endpoint I call Gurobipy in a separate thread to solve a MIP problem. Due to the hardware where the API and solver are installed I can't have more then a single istance of the model running so I would like to have the possibility to abort the currently running model with another endpoint.
At the moment I tried rising an excpetion in the thread where the MIP problem is being solved but a KeyboardInterrupt exception doesn't produce any results. The solver ignores the exception, returns the following message and proceeds with the solving process:
Exception ignored in:
'gurobipy.logcallbackstub'
I've also tried to catch the exception in a callback function as follows but I'me getting the exact same result:
from gurobipy import GRB
import sys
def my_callback(model, where):
if where == GRB.Callback.POLLING:
if sys.exc_info()[0] is not None:
model.terminate()
Is there a way to terminate the optimization process early from an exception similarly at what can be done with Ctrl+C when running the solving process in a terminal? And if this is not the correct way how can a graceful termination be achived when running Gurobipy in a different process?
-
Hi Lorenzo,
You said that you are calling Gurobi in a separate thread. If you have a reference to the model object available in your main thread then you could just call model.terminate() from there leading to a clean termination of the solution process in the other thread. Then you could even access solution information etc.
Or are you required to handle this via exceptions?
Best regards,
Mario1 -
Dear Mario,
Thank you for you reply. Unfortunately I don't have a direct reference to the model: this is why I tried catching the exception "manually" in a callback where I could model.terminate(), having a direct reference to the model there.
In our usecase we have a single concurrent user Gurobi license and we use a task queue to be able to schedule more models that will be evaluated one after the other.
Rising an exception succesfully aborts queued models and also models that are being built, but as soon as the model start being solved no regular Python exception seems to be handled by the gurobipy library.
0 -
Hi Lorenzo,
We had a discussion with our developers and the following is the answer my colleague Silke already told your colleague Luca. I just add it here for completeness:
"The exception is ignored simply because Gurobi does not add any signal handlers for this. We only handle signals like Crtl+C in the command line or the interactive mode, but in situations like yours it is the user's responsibility to install a signal handler. However, using signals is not a recommended way for interprocess communication in general. Instead you could use either shared memory (for threads) or a socket which is written to from the parent and then checked by the child process/thread in the POLLING callback."
Best regards,
Mario0
サインインしてコメントを残してください。
コメント
3件のコメント