from simgrid import Engine, this_actor import sys class Sleeper: """This actor just sleeps until termination""" def __init__(self, *args): # sys.exit(1); simgrid.info("Exiting now (done sleeping or got killed).")) this_actor.on_exit(lambda: print("BAAA")) def __call__(self): this_actor.info("Hello! I go to sleep.") this_actor.sleep_for(10) this_actor.info("Done sleeping.") if __name__ == '__main__': e = Engine(sys.argv) if len(sys.argv) < 2: raise AssertionError( "Usage: actor-lifetime.py platform_file actor-lifetime_d.xml [other parameters]" ) e.load_platform(sys.argv[1]) # Load the platform description e.register_actor("sleeper", Sleeper) # Deploy the sleeper processes with explicit start/kill times e.load_deployment(sys.argv[2]) e.run()
class Receiver: def __init__(self, *args): if len(args) != 1: # Receiver actor expects 1 argument: its ID raise AssertionError( "Actor receiver requires 1 parameter, but got {:d}".format(len(args))) self.mbox = Mailbox.by_name("receiver-{:s}".format(args[0])) def __call__(self): this_actor.info("Wait for my first message") while True: received = self.mbox.get() this_actor.info("I got a '{:s}'.".format(received)) if received == "finalize": break # If it's a finalize message, we're done. if __name__ == '__main__': e = Engine(sys.argv) # Load the platform description e.load_platform(sys.argv[1]) # Register the classes representing the actors e.register_actor("sender", Sender) e.register_actor("receiver", Receiver) e.load_deployment(sys.argv[2]) e.run()
def __call__(self): this_actor.info("Hello s4u, I have something to send") mailbox = Mailbox.by_name(self.mbox) mailbox.put(self.msg, len(self.msg)) this_actor.info("I'm done. See you.") if __name__ == '__main__': # Here comes the main function of your program # When your program starts, you have to first start a new simulation engine, as follows e = Engine(sys.argv) # Then you should load a platform file, describing your simulated platform e.load_platform("../../platforms/small_platform.xml") # And now you have to ask SimGrid to actually start your actors. # # The easiest way to do so is to implement the behavior of your actor in a single function, # as we do here for the receiver actors. This function can take any kind of parameters, as # long as the last parameters of Actor::create() match what your function expects. Actor.create("receiver", Host.by_name("Fafard"), receiver, "mb42") # If your actor is getting more complex, you probably want to implement it as a class instead, # as we do here for the sender actors. The main behavior goes into operator()() of the class. # # You can then directly start your actor, as follows: Actor.create("sender1", Host.by_name("Tremblay"), Sender()) # If you want to pass parameters to your class, that's very easy: just use your constructors Actor.create("sender2", Host.by_name("Jupiter"), Sender("GloubiBoulga"))