How to add event handlers #193
-
|
I am trying to add an event handler to an environment that keeps track of the dual bound history.
You can find a slimmed down version of my code that produces the same error here. I can not find an error in my approach, but maybe I am doing something wrong. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @CharJon, I think I understand what is going on. Python has a Global Interpreter Lock (GIL) that constraint it to be single threaded. When executing native (e.g. C/C++) code, this lock can be lifted to allow threading, which is what Ecole does. PySCIPOpt however does not make this assumption. So when Ecole solves the I have send a PR to PySCIPOpt to ensure the GIL is locked (you can already use it without it getting merged). ReferenceError Traceback (most recent call last)
<ipython-input-2-367f052e7cd2> in eventinit(self)
23
24 def eventinit(self):
---> 25 self.model.catchEvent(scip.SCIP_EVENTTYPE.LPEVENT, self)
26
27 def eventexit(self):
ReferenceError: weakly-referenced object no longer existsTo avoid it, you need to keep a reference to the PySCIPOpt Model like so: def before_reset(self, model):
self.pysicpopt_model = model.as_pyscipopt()
self.pysicpopt_model.includeEventhdlr(self.dual_bound_handler, "Some handler", "Its description") |
Beta Was this translation helpful? Give feedback.
Hi @CharJon,
I think I understand what is going on. Python has a Global Interpreter Lock (GIL) that constraint it to be single threaded. When executing native (e.g. C/C++) code, this lock can be lifted to allow threading, which is what Ecole does.
PySCIPOpt however does not make this assumption. So when Ecole solves the
Model, the Python event handler eventually gets called but the GIL is never locked...I have send a PR to PySCIPOpt to ensure the GIL is locked (you can already use it without it getting merged).
It should work, but our PySCIPOpt interface in Ecole still has a few rough edges... For instance, in your
DualBoundObservation, you would now getReferenceError …