Logo AND Algorithmique Numérique Distribuée

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