From: Martin Quinson Date: Thu, 3 Jan 2019 21:37:56 +0000 (+0100) Subject: py: another example: actor-join X-Git-Tag: v3_22~707 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/429997d6c6c0bee1f8bba49be7277a17c3595318 py: another example: actor-join --- diff --git a/examples/python/CMakeLists.txt b/examples/python/CMakeLists.txt index 435245da99..b230d307c0 100644 --- a/examples/python/CMakeLists.txt +++ b/examples/python/CMakeLists.txt @@ -1,4 +1,5 @@ -foreach(example actor-create actor-migration actor-yield exec-basic) +foreach(example actor-create actor-join actor-migration actor-yield + exec-basic) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.tesh) set(examples_src ${examples_src} ${CMAKE_CURRENT_SOURCE_DIR}/${example}/${example}.py) diff --git a/examples/python/actor-join/actor-join.py b/examples/python/actor-join/actor-join.py new file mode 100644 index 0000000000..bf8cfc24b9 --- /dev/null +++ b/examples/python/actor-join/actor-join.py @@ -0,0 +1,53 @@ +# Copyright (c) 2017-2018. The SimGrid Team. All rights reserved. +# +# This program is free software you can redistribute it and/or modify it +# under the terms of the license (GNU LGPL) which comes with this package. + +from simgrid import * +import sys + +def sleeper(): + this_actor.info("Sleeper started") + this_actor.sleep_for(3) + this_actor.info("I'm done. See you!") + +def master(): + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 2)") + actor.join(2) + + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 4)") + actor.join(4) + + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 2)") + actor.join(2) + + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Waiting 4") + this_actor.sleep_for(4) + this_actor.info("Join the sleeper after its end (timeout 1)") + actor.join(1) + + this_actor.info("Goodbye now!") + + this_actor.sleep_for(1) + + this_actor.info("Goodbye now!") + +if __name__ == '__main__': + e = Engine(sys.argv) + if len(sys.argv) < 2: raise AssertionError("Usage: actor-join.py platform_file [other parameters]") + + e.load_platform(sys.argv[1]) + + Actor.create("master", Host.by_name("Tremblay"), master) + + e.run() + + this_actor.info("Simulation time {}".format(Engine.get_clock())) diff --git a/examples/python/actor-join/actor-join.tesh b/examples/python/actor-join/actor-join.tesh new file mode 100644 index 0000000000..4fc199e1db --- /dev/null +++ b/examples/python/actor-join/actor-join.tesh @@ -0,0 +1,21 @@ +$ python3 ${bindir:=.}/actor-join.py ${platfdir}/small_platform.xml +> [Tremblay:master:(1) 0.000000] [python/INFO] Start sleeper +> [Tremblay:sleeper from master:(2) 0.000000] [python/INFO] Sleeper started +> [Tremblay:master:(1) 0.000000] [python/INFO] Join the sleeper (timeout 2) +> [Tremblay:master:(1) 2.000000] [python/INFO] Start sleeper +> [Tremblay:sleeper from master:(3) 2.000000] [python/INFO] Sleeper started +> [Tremblay:master:(1) 2.000000] [python/INFO] Join the sleeper (timeout 4) +> [Tremblay:sleeper from master:(2) 3.000000] [python/INFO] I'm done. See you! +> [Tremblay:sleeper from master:(3) 5.000000] [python/INFO] I'm done. See you! +> [Tremblay:master:(1) 5.000000] [python/INFO] Start sleeper +> [Tremblay:sleeper from master:(4) 5.000000] [python/INFO] Sleeper started +> [Tremblay:master:(1) 5.000000] [python/INFO] Join the sleeper (timeout 2) +> [Tremblay:master:(1) 7.000000] [python/INFO] Start sleeper +> [Tremblay:sleeper from master:(5) 7.000000] [python/INFO] Sleeper started +> [Tremblay:master:(1) 7.000000] [python/INFO] Waiting 4 +> [Tremblay:sleeper from master:(4) 8.000000] [python/INFO] I'm done. See you! +> [Tremblay:sleeper from master:(5) 10.000000] [python/INFO] I'm done. See you! +> [Tremblay:master:(1) 11.000000] [python/INFO] Join the sleeper after its end (timeout 1) +> [Tremblay:master:(1) 11.000000] [python/INFO] Goodbye now! +> [Tremblay:master:(1) 12.000000] [python/INFO] Goodbye now! +> [12.000000] [python/INFO] Simulation time 12.0 diff --git a/examples/s4u/README.rst b/examples/s4u/README.rst index 6d45a6a725..48bded29fd 100644 --- a/examples/s4u/README.rst +++ b/examples/s4u/README.rst @@ -75,9 +75,11 @@ Inter-Actors Interactions :py:func:`simgrid.this_actor.migrate()` |py| - **Waiting for the termination of an actor:** (joining on it) - :cpp:func:`simgrid::s4u::Actor::join()` allows to block the current - actor until the end of the receiving actor. - |br| `examples/s4u/actor-join/s4u-actor-join.cpp `_ + You can block the current actor until the end of another actor. + |br| |cpp| `examples/s4u/actor-join/s4u-actor-join.cpp `_ + :cpp:func:`simgrid::s4u::Actor::join()` |cpp| + |br| |py| `examples/python/actor-join/actor-join.py `_ + :cpp:func:`simgrid.Actor.join()` |py| - **Yielding to other actors**. The ```yield()``` function interrupts the execution of the current diff --git a/include/simgrid/s4u/Actor.hpp b/include/simgrid/s4u/Actor.hpp index 64fd9bcf28..87b0082596 100644 --- a/include/simgrid/s4u/Actor.hpp +++ b/include/simgrid/s4u/Actor.hpp @@ -268,9 +268,16 @@ public: /** Wait for the actor to finish. * - * This blocks the calling actor until the actor on which we call join() is terminated + * Blocks the calling actor until the joined actor is terminated. If actor alice executes bob.join(), then alice is + * blocked until bob terminates. */ void join(); + + /** Wait for the actor to finish, or for the timeout to elapse. + * + * Blocks the calling actor until the joined actor is terminated. If actor alice executes bob.join(), then alice is + * blocked until bob terminates. + */ void join(double timeout); Actor* restart(); diff --git a/include/simgrid/s4u/Host.hpp b/include/simgrid/s4u/Host.hpp index 332ae45c94..88ac89dcf8 100644 --- a/include/simgrid/s4u/Host.hpp +++ b/include/simgrid/s4u/Host.hpp @@ -69,7 +69,7 @@ public: static Host* by_name_or_null(std::string name); /** Retrieve a host from its name, or die */ static s4u::Host* by_name(std::string name); - /** Retrieve the host on which the current actor is running */ + /** Retrieves the host on which the running actor is located */ static s4u::Host* current(); /** Retrieves the name of that host as a C++ string */ diff --git a/src/bindings/python/simgrid_python.cpp b/src/bindings/python/simgrid_python.cpp index 68fb1233f1..d42f180245 100644 --- a/src/bindings/python/simgrid_python.cpp +++ b/src/bindings/python/simgrid_python.cpp @@ -79,6 +79,7 @@ PYBIND11_MODULE(simgrid, m) // Currently this can be dangling, we should wrap this somehow. return new simgrid::s4u::Engine(&argc, argv.get()); })) + .def("get_clock", &Engine::get_clock, "Retrieve the simulation time") .def("load_platform", &Engine::load_platform, "Load a platform file describing the environment, see :cpp:func:`simgrid::s4u::Engine::load_platform()`") .def("load_deployment", &Engine::load_deployment, @@ -109,7 +110,8 @@ PYBIND11_MODULE(simgrid, m) /* Class Host */ py::class_>(m, "Host", "Simulation Engine, see :ref:`class s4u::Host `") - .def("by_name", &Host::by_name, "Retrieve a host from its name, or die") + .def("by_name", &Host::by_name, "Retrieves a host from its name, or die") + .def("current", &Host::current, "Retrieves the host on which the running actor is located, see :cpp:func:`simgrid::s4u::Host::current()`") .def_property_readonly("name", [](Host* self) -> const std::string { return static_cast(self->get_name()); }, "Retrieve the name of this host") @@ -152,8 +154,10 @@ PYBIND11_MODULE(simgrid, m) }, "Create an actor from a function or an object, see :cpp:func:`simgrid::s4u::Actor::create()`") .def_property("host", &Actor::get_host, &Actor::migrate, "The host on which this actor is located") + .def("join", py::overload_cast(&Actor::join), "Wait for the actor to finish, see :cpp:func:`void simgrid::s4u::Actor::join(double)`", + py::arg("timeout")) .def("migrate", &Actor::migrate, "Moves that actor to another host, see :cpp:func:`void simgrid::s4u::Actor::migrate()`", - py::arg("dest")) + py::arg("dest")) .def("suspend", &Actor::suspend, "Suspend that actor, that is blocked until resume()ed by another actor. See :cpp:func:`void simgrid::s4u::Actor::suspend()`") .def("resume", &Actor::resume, "Resume that actor, that was previously suspend()ed. See :cpp:func:`void simgrid::s4u::Actor::suspend()`");