Logo AND Algorithmique Numérique Distribuée

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