From: Martin Quinson Date: Wed, 2 Mar 2022 17:35:36 +0000 (+0100) Subject: Pass the boolean parameter to the on_exit python binding X-Git-Tag: v3.31~248 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/0de8df10470bd86136bd9c2f4eb716f604c3ca07 Pass the boolean parameter to the on_exit python binding --- diff --git a/examples/python/actor-kill/actor-kill.py b/examples/python/actor-kill/actor-kill.py index 5adff3112d..fa2871723d 100644 --- a/examples/python/actor-kill/actor-kill.py +++ b/examples/python/actor-kill/actor-kill.py @@ -12,7 +12,7 @@ from simgrid import Actor, Engine, Host, this_actor def victim_a_fun(): - this_actor.on_exit(lambda: this_actor.info("I have been killed!")) + this_actor.on_exit(lambda forcefully: this_actor.info("I have been killed!" if forcefully else "I finish now.")) this_actor.info("Hello!") this_actor.info("Suspending myself") this_actor.suspend() # - Start by suspending itself diff --git a/examples/python/actor-lifetime/actor-lifetime.py b/examples/python/actor-lifetime/actor-lifetime.py index a8dec3f25b..7cd8867d11 100644 --- a/examples/python/actor-lifetime/actor-lifetime.py +++ b/examples/python/actor-lifetime/actor-lifetime.py @@ -16,7 +16,7 @@ class Sleeper: """This actor just sleeps until termination""" def __init__(self): - this_actor.on_exit(lambda: this_actor.info("Exiting now (done sleeping or got killed).")) + this_actor.on_exit(lambda killed: this_actor.info("Exiting now (killed)." if killed else "Exiting now (finishing).")) def __call__(self): this_actor.info("Hello! I go to sleep.") diff --git a/examples/python/actor-lifetime/actor-lifetime.tesh b/examples/python/actor-lifetime/actor-lifetime.tesh index bf67b9de50..9ac2ae7f09 100644 --- a/examples/python/actor-lifetime/actor-lifetime.tesh +++ b/examples/python/actor-lifetime/actor-lifetime.tesh @@ -4,11 +4,11 @@ $ ${pythoncmd:=python3} ${PYTHON_TOOL_OPTIONS:=} ${bindir:=.}/actor-lifetime.py > [ 0.000000] (1:sleeper@node-0.simgrid.org) Hello! I go to sleep. > [ 0.000000] (2:sleeper@node-1.simgrid.org) Hello! I go to sleep. > [ 2.000000] (3:sleeper@node-0.simgrid.org) Hello! I go to sleep. -> [ 3.000000] (2:sleeper@node-1.simgrid.org) Exiting now (done sleeping or got killed). +> [ 3.000000] (2:sleeper@node-1.simgrid.org) Exiting now (killed). > [ 4.000000] (4:sleeper@node-2.simgrid.org) Hello! I go to sleep. -> [ 7.000000] (4:sleeper@node-2.simgrid.org) Exiting now (done sleeping or got killed). +> [ 7.000000] (4:sleeper@node-2.simgrid.org) Exiting now (killed). > [ 10.000000] (1:sleeper@node-0.simgrid.org) Done sleeping. -> [ 10.000000] (1:sleeper@node-0.simgrid.org) Exiting now (done sleeping or got killed). +> [ 10.000000] (1:sleeper@node-0.simgrid.org) Exiting now (finishing). > [ 12.000000] (3:sleeper@node-0.simgrid.org) Done sleeping. -> [ 12.000000] (3:sleeper@node-0.simgrid.org) Exiting now (done sleeping or got killed). +> [ 12.000000] (3:sleeper@node-0.simgrid.org) Exiting now (finishing). diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index 96e067bf50..206f1d0c6f 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -126,16 +126,18 @@ PYBIND11_MODULE(simgrid, m) [](py::object cb) { py::function fun = py::reinterpret_borrow(cb); fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime? - simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) { + simgrid::s4u::this_actor::on_exit([fun](bool failed) { py::gil_scoped_acquire py_context; // need a new context for callback try { - fun(); + fun(failed); } catch (const py::error_already_set& e) { xbt_die("Error while executing the on_exit lambda: %s", e.what()); } }); }, - py::call_guard(), "") + py::call_guard(), + "Define a lambda to be called when the actor ends. It takes a bool parameter indicating whether the actor " + "was killed. If False, the actor finished peacefully.") .def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor") .def("get_ppid", &simgrid::s4u::this_actor::get_ppid, "Retrieves PPID of the current actor (i.e., the PID of its parent).");