Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / python / actor-kill / actor-kill.py
1 # Copyright (c) 2017-2022. The SimGrid Team. All rights reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the license (GNU LGPL) which comes with this package.
5
6 from simgrid import Actor, Engine, Host, this_actor
7 import sys
8
9
10 def victim_a_fun():
11     this_actor.on_exit(lambda: this_actor.info("I have been killed!"))
12     this_actor.info("Hello!")
13     this_actor.info("Suspending myself")
14     this_actor.suspend()                        # - Start by suspending itself
15     # - Then is resumed and start to execute a task
16     this_actor.info("OK, OK. Let's work")
17     this_actor.execute(1e9)
18     # - But will never reach the end of it
19     this_actor.info("Bye!")
20
21
22 def victim_b_fun():
23     this_actor.info("Terminate before being killed")
24
25
26 def killer():
27     this_actor.info("Hello!")  # - First start a victim actor
28     victim_a = Actor.create("victim A", Host.by_name("Fafard"), victim_a_fun)
29     victim_b = Actor.create("victim B", Host.by_name("Jupiter"), victim_b_fun)
30     this_actor.sleep_for(10)  # - Wait for 10 seconds
31
32     # - Resume it from its suspended state
33     this_actor.info("Resume the victim A")
34     victim_a.resume()
35     this_actor.sleep_for(2)
36
37     this_actor.info("Kill the victim A")   # - and then kill it
38     Actor.by_pid(victim_a.pid).kill()       # You can retrieve an actor from its PID (and then kill it)
39
40     this_actor.sleep_for(1)
41
42     # that's a no-op, there is no zombies in SimGrid
43     this_actor.info("Kill victim B, even if it's already dead")
44     victim_b.kill()
45
46     this_actor.sleep_for(1)
47
48     this_actor.info("Start a new actor, and kill it right away")
49     victim_c = Actor.create("victim C", Host.by_name("Jupiter"), victim_a_fun)
50     victim_c.kill()
51
52     this_actor.sleep_for(1)
53
54     this_actor.info("Killing everybody but myself")
55     Actor.kill_all()
56
57     this_actor.info("OK, goodbye now. I commit a suicide.")
58     this_actor.exit()
59
60     this_actor.info(
61         "This line never gets displayed: I'm already dead since the previous line.")
62
63
64 if __name__ == '__main__':
65     e = Engine(sys.argv)
66     if len(sys.argv) < 2:
67         raise AssertionError(
68             "Usage: actor-kill.py platform_file [other parameters]")
69
70     e.load_platform(sys.argv[1])     # Load the platform description
71     # Create and deploy killer actor, that will create the victim actors
72     Actor.create("killer", Host.by_name("Tremblay"), killer)
73
74     e.run()