Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
typo
[simgrid.git] / examples / python / actor-create / actor-create.py
1 # Copyright (c) 2006-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 # This example shows how to declare and start your actors.
7 #
8 # The first step is to declare the code of your actors (what they do exactly does not matter to this example) and then
9 # you ask SimGrid to start your actors. There is three ways of doing so:
10 # - Directly, by instantiating your actor as parameter to Actor::create()
11 # - By first registering your actors before instantiating it;
12 # - Through the deployment file.
13 #
14 # This example shows all these solutions, even if you obviously should use only one of these solutions to start your
15 # actors. The most advised solution is to use a deployment file, as it creates a clear separation between your
16 # application and the settings to test it. This is a better scientific methodology. Actually, starting an actor with
17 # Actor.create() is mostly useful to start an actor from another actor.
18
19 import sys
20 from simgrid import *
21
22 # Our first class of actors is simply implemented with a function, that takes a single string as parameter.
23 #
24 # Later, this actor class is instantiated within the simulation.
25 def receiver(mailbox_name):
26   mailbox = Mailbox.by_name(mailbox_name)
27
28   this_actor.info("Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.get_name()))
29
30   msg1 = mailbox.get()
31   msg2 = mailbox.get()
32   msg3 = mailbox.get()
33   this_actor.info("I received '{:s}', '{:s}' and '{:s}'".format(msg1, msg2, msg3))
34   this_actor.info("I'm done. See you.")
35
36 # Our second class of actors is also a function
37 def forwarder(*args):
38   if len(args) < 2: raise AssertionError("Actor forwarder requires 2 parameters, but got only {:d}".format(len(args)))
39   mb_in  = Mailbox.by_name(args[0])
40   mb_out = Mailbox.by_name(args[1])
41
42   msg = mb_in.get()
43   this_actor.info("Forward '{:s}'.".format(msg))
44   mb_out.put(msg, len(msg))
45
46 # Declares a third class of actors which sends a message to the mailbox 'mb42'.
47 # The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default)
48 #
49 # Later, this actor class is instantiated twice in the simulation.
50 class Sender:
51   mbox  = "mb42"
52   msg = "GaBuZoMeu";
53   def __init__(self, *args):
54       if len(args) > 0: self.msg  = args[0];
55       if len(args) > 1: self.mbox = args[1];
56       if len(args) > 2: raise AssertionError("Actor sender requires 2 parameters, but got only {:d}".format(len(args)))
57
58   def __call__(self):
59       this_actor.info("Hello s4u, I have something to send")
60       mailbox = Mailbox.by_name(self.mbox)
61
62       mailbox.put(self.msg, len(self.msg))
63       this_actor.info("I'm done. See you.")
64
65 # Here comes the main function of your program
66 if __name__ == '__main__':
67   # When your program starts, you have to first start a new simulation engine, as follows
68   e = Engine(sys.argv)
69
70   # Then you should load a platform file, describing your simulated platform
71   e.load_platform("../../platforms/small_platform.xml");
72
73   # And now you have to ask SimGrid to actually start your actors.
74   #
75   # The easiest way to do so is to implement the behavior of your actor in a single function,
76   # as we do here for the receiver actors. This function can take any kind of parameters, as
77   # long as the last parameters of Actor::create() match what your function expects.
78   Actor.create("receiver", Host.by_name("Fafard"), receiver, "mb42")
79
80   # If your actor is getting more complex, you probably want to implement it as a class instead,
81   # as we do here for the sender actors. The main behavior goes into operator()() of the class.
82   #
83   # You can then directly start your actor, as follows:
84   Actor.create("sender1", Host.by_name("Tremblay"), Sender())
85   # If you want to pass parameters to your class, that's very easy: just use your constructors
86   Actor.create("sender2", Host.by_name("Jupiter"), Sender("GloubiBoulga"));
87
88   # But starting actors directly is considered as a bad experimental habit, since it ties the code
89   # you want to test with the experimental scenario. Starting your actors from an external deployment
90   # file in XML ensures that you can test your code in several scenarios without changing the code itself.
91   #
92   # For that, you first need to register your function or your actor as follows.
93   e.register_actor("sender", Sender)
94   e.register_actor("forwarder", forwarder)
95   # Once actors and functions are registered, just load the deployment file
96   e.load_deployment("actor-create_d.xml")
97
98   # Once every actors are started in the engine, the simulation can start
99   e.run();
100
101   # Once the simulation is done, the program is ended