Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / python / actor-suspend / actor-suspend.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-suspend.py platform_file [other parameters]
8 """
9
10 import sys
11 from simgrid import Actor, Engine, this_actor
12
13
14 def lazy_guy():
15     """The Lazy guy only wants to sleep, but can be awaken by the dream_master actor"""
16     this_actor.info("Nobody's watching me ? Let's go to sleep.")
17     this_actor.suspend()  # - Start by suspending itself
18     this_actor.info("Uuuh ? Did somebody call me ?")
19
20     # - Then repetitively go to sleep, but get awaken
21     this_actor.info("Going to sleep...")
22     this_actor.sleep_for(10)
23     this_actor.info("Mmm... waking up.")
24
25     this_actor.info("Going to sleep one more time (for 10 sec)...")
26     this_actor.sleep_for(10)
27     this_actor.info("Waking up once for all!")
28
29     this_actor.info("Ok, let's do some work, then (for 10 sec on Boivin).")
30     this_actor.execute(980.95e6)
31
32     this_actor.info("Mmmh, I'm done now. Goodbye.")
33
34
35 def dream_master():
36     """The Dream master"""
37     this_actor.info("Let's create a lazy guy.")  # Create a lazy_guy actor
38     lazy = Actor.create("Lazy", this_actor.get_host(), lazy_guy)
39     this_actor.info("Let's wait a little bit...")
40     this_actor.sleep_for(10)  # Wait for 10 seconds
41     this_actor.info("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!")
42     if lazy.is_suspended():
43         lazy.resume()  # Then wake up the lazy_guy
44     else:
45         this_actor.error(
46             "I was thinking that the lazy guy would be suspended now")
47
48     this_actor.sleep_for(5)  # Repeat two times:
49     this_actor.info("Suspend the lazy guy while he's sleeping...")
50     lazy.suspend()  # Suspend the lazy_guy while he's asleep
51     this_actor.info("Let him finish his siesta.")
52     this_actor.sleep_for(10)  # Wait for 10 seconds
53     this_actor.info("Wake up, lazy guy!")
54     lazy.resume()  # Then wake up the lazy_guy again
55
56     this_actor.sleep_for(5)
57     this_actor.info("Suspend again the lazy guy while he's sleeping...")
58     lazy.suspend()
59     this_actor.info("This time, don't let him finish his siesta.")
60     this_actor.sleep_for(2)
61     this_actor.info("Wake up, lazy guy!")
62     lazy.resume()
63
64     this_actor.sleep_for(5)
65     this_actor.info(
66         "Give a 2 seconds break to the lazy guy while he's working...")
67     lazy.suspend()
68     this_actor.sleep_for(2)
69     this_actor.info("Back to work, lazy guy!")
70     lazy.resume()
71
72     this_actor.info("OK, I'm done here.")
73
74
75 if __name__ == '__main__':
76     e = Engine(sys.argv)
77     if len(sys.argv) < 2:
78         raise AssertionError(
79             "Usage: actor-suspend.py platform_file [other parameters]")
80
81     e.load_platform(sys.argv[1])  # Load the platform description
82     hosts = e.all_hosts
83     Actor.create("dream_master", hosts[0], dream_master)
84
85     e.run()  # Run the simulation