Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[python/pep8] Avoid wildcard imports.
[simgrid.git] / examples / python / actor-yield / actor-yield.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 Engine, this_actor
7 import sys
8
9 # This example does not much: It just spans over-polite actor that yield a large amount
10 # of time before ending.
11 #
12 # This serves as an example for the simgrid.yield() function, with which an actor can request
13 # to be rescheduled after the other actor that are ready at the current timestamp.
14 #
15 # It can also be used to benchmark our context-switching mechanism.
16
17
18 class Yielder:
19     """Main function of the Yielder process"""
20     number_of_yields = 0
21
22     def __init__(self, *args):
23         self.number_of_yields = int(args[0])
24
25     def __call__(self):
26         for _ in range(self.number_of_yields):
27             this_actor.yield_()
28         this_actor.info("I yielded {:d} times. Goodbye now!".format(
29             self.number_of_yields))
30
31
32 if __name__ == '__main__':
33     e = Engine(sys.argv)
34
35     e.load_platform(sys.argv[1])             # Load the platform description
36     # Register the class representing the actors
37     e.register_actor("yielder", Yielder)
38
39     e.load_deployment(sys.argv[2])
40
41     e.run()  # - Run the simulation