def forwarder(*args): """Our second class of actors is also a function""" if len(args) < 2: raise AssertionError( "Actor forwarder requires 2 parameters, but got only {:d}".format(len(args))) mb_in = Mailbox.by_name(args[0]) mb_out = Mailbox.by_name(args[1]) msg = mb_in.get() this_actor.info("Forward '{:s}'.".format(msg)) mb_out.put(msg, len(msg))
def __call__(self): # List in which we store all ongoing communications pending_comms = [] # Vector of the used mailboxes mboxes = [ Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, self.receivers_count) ] # Start dispatching all messages to receivers, in a round robin fashion for i in range(0, self.messages_count): content = "Message {:d}".format(i) mbox = mboxes[i % self.receivers_count] this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox))) # Create a communication representing the ongoing communication, and store it in pending_comms comm = mbox.put_async(content, self.msg_size) pending_comms.append(comm) # Start sending messages to let the workers know that they should stop for i in range(0, self.receivers_count): mbox = mboxes[i] this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox))) comm = mbox.put_async("finalize", 0) pending_comms.append(comm) this_actor.info("Done dispatching all messages") # Now that all message exchanges were initiated, wait for their completion in one single call Comm.wait_all(pending_comms) this_actor.info("Goodbye now!")
def __call__(self): # FIXME: It should be ok to initialize self.mbox from __init__, but it's currently failing on the OS X Jenkins slave. self.mbox = Mailbox.by_name("receiver-{:d}".format(self.id)) 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.
def __call__(self): this_actor.execute(1e9) for disk in Host.current().get_disks(): this_actor.info("Using disk " + disk.name) disk.read(10000) disk.write(10000) mbox = Mailbox.by_name(this_actor.get_host().name) msg = mbox.get() this_actor.info("I got '%s'." % msg) this_actor.info("Finished executing. Goodbye!")
def __call__(self): for host in self.hosts: mbox = Mailbox.by_name(host.name) msg = "Hello. I'm " + str(this_actor.get_host().name) size = int(1e6) this_actor.info("Sending msg to " + host.name) mbox.put(msg, size) this_actor.info("Done dispatching all messages. Goodbye!")
def receiver(mailbox_name): """ Our first class of actors is simply implemented with a function, that takes a single string as parameter. Later, this actor class is instantiated within the simulation. """ mailbox = Mailbox.by_name(mailbox_name) this_actor.info( "Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.name)) msg1 = mailbox.get() msg2 = mailbox.get() msg3 = mailbox.get() this_actor.info( "I received '{:s}', '{:s}' and '{:s}'".format(msg1, msg2, msg3)) this_actor.info("I'm done. See you.")
def __call__(self): mbox = Mailbox.by_name("receiver") pending_msgs = [] pending_comms = [] this_actor.info("Wait for %d messages asynchronously" % self.msg_count) for _ in range(self.msg_count): comm, data = mbox.get_async() pending_comms.append(comm) pending_msgs.append(data) while len(pending_comms) > 0: index = Comm.wait_any(pending_comms) msg = pending_msgs[index].get() this_actor.info("I got '%s'." % msg) del pending_comms[index] del pending_msgs[index]
def __call__(self): pending_comms = [] mbox = Mailbox.by_name("receiver") for i in range(self.msg_count): msg = "Message " + str(i) size = self.msg_size * (i + 1) this_actor.info("Send '%s' to '%s, msg size: %d'" % (msg, mbox.name, size)) comm = mbox.put_async(msg, size) pending_comms.append(comm) this_actor.info("Done dispatching all messages") # Now that all message exchanges were initiated, wait for their completion in one single call Comm.wait_all(pending_comms) this_actor.info("Goodbye now!")
def __call__(self): # List in which we store all ongoing communications pending_comms = [] # Vector of the used mailboxes mboxes = [ Mailbox.by_name("receiver-{:d}".format(i)) for i in range(0, self.receivers_count) ] # Start dispatching all messages to receivers, in a round robin fashion for i in range(0, self.messages_count): content = "Message {:d}".format(i) mbox = mboxes[i % self.receivers_count] this_actor.info("Send '{:s}' to '{:s}'".format(content, str(mbox))) # Create a communication representing the ongoing communication, and store it in pending_comms comm = mbox.put_async(content, self.msg_size) pending_comms.append(comm) # Start sending messages to let the workers know that they should stop for i in range(0, self.receivers_count): mbox = mboxes[i] this_actor.info("Send 'finalize' to '{:s}'".format(str(mbox))) comm = mbox.put_async("finalize", 0) pending_comms.append(comm) this_actor.info("Done dispatching all messages") # Now that all message exchanges were initiated, wait for their completion, in order of completion. # # This loop waits for first terminating message with wait_any() and remove it with del, until all comms are # terminated. # Even in this simple example, the pending comms do not terminate in the exact same order of creation. while pending_comms: changed_pos = Comm.wait_any(pending_comms) del pending_comms[changed_pos] if (changed_pos != 0): this_actor.info( "Remove the {:d}th pending comm: it terminated earlier than another comm that was initiated first." .format(changed_pos)) this_actor.info("Goodbye now!")
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("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.")