Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pass the boolean parameter to the on_exit python binding
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 2 Mar 2022 17:35:36 +0000 (18:35 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Wed, 2 Mar 2022 23:57:21 +0000 (00:57 +0100)
examples/python/actor-kill/actor-kill.py
examples/python/actor-lifetime/actor-lifetime.py
examples/python/actor-lifetime/actor-lifetime.tesh
src/bindings/python/simgrid_python.cpp

index 5adff31..fa28717 100644 (file)
@@ -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
index a8dec3f..7cd8867 100644 (file)
@@ -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.")
index bf67b9d..9ac2ae7 100644 (file)
@@ -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).
 
index 96e067b..206f1d0 100644 (file)
@@ -126,16 +126,18 @@ PYBIND11_MODULE(simgrid, m)
           [](py::object cb) {
             py::function fun = py::reinterpret_borrow<py::function>(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::gil_scoped_release>(), "")
+          py::call_guard<py::gil_scoped_release>(),
+          "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).");