Logo AND Algorithmique Numérique Distribuée

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