1 # Copyright (c) 2010-2023. The SimGrid Team. All rights reserved.
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.
7 This example shows how to block on the completion of a set of communications.
9 As for the other asynchronous examples, the sender initiate all the messages it wants to send and
10 pack the resulting simgrid.Comm objects in a list. All messages thus occur concurrently.
12 The sender then loops until there is no ongoing communication. Using wait_any() ensures that the sender
13 will notice events as soon as they occur even if it does not follow the order of the container.
15 Here, finalize messages will terminate earlier because their size is 0, so they travel faster than the
16 other messages of this application. As expected, the trace shows that the finalize of worker 1 is
17 processed before 'Message 5' that is sent to worker 0.
21 from simgrid import Actor, Comm, Engine, Host, Mailbox, this_actor
24 FINALIZE_TAG = "finalize"
27 def sender(receiver_id: str, messages_count: int, payload_size: int) -> None:
28 # List in which we store all ongoing communications
30 mbox = Mailbox.by_name(receiver_id)
32 # Asynchronously send `messages_count` message(s) to the receiver
33 for i in range(0, messages_count):
34 payload = "Message {:d}".format(i)
35 this_actor.info("Send '{:s}' to '{:s}'".format(payload, receiver_id))
37 # Create a communication representing the ongoing communication, and store it in pending_comms
38 comm = mbox.put_async(payload, payload_size)
39 pending_comms.append(comm)
41 # Send the final message to the receiver
42 payload = FINALIZE_TAG
43 final_payload_size = 0
44 final_comm = mbox.put_async(payload, final_payload_size)
45 pending_comms.append(final_comm)
46 this_actor.info("Send '{:s}' to '{:s}".format(payload, receiver_id))
47 this_actor.info("Done dispatching all messages")
49 this_actor.info("Waiting for all outstanding communications to complete")
51 current_comm: Comm = pending_comms[-1]
52 current_comm.wait_for(1.0)
54 this_actor.info("Goodbye now!")
57 def receiver(identifier: str) -> None:
58 mbox: Mailbox = Mailbox.by_name(identifier)
59 this_actor.info("Wait for my first message")
62 this_actor.info("I got a '{:s}'.".format(received))
63 if received == FINALIZE_TAG:
65 this_actor.info("Goodbye now!")
71 # Load the platform description
72 e.load_platform(sys.argv[1])
74 receiver_id = "receiver-0"
75 Actor.create("sender", Host.by_name("Tremblay"), sender, receiver_id, 3, int(5e7))
76 Actor.create("receiver", Host.by_name("Ruby"), receiver, receiver_id)
81 if __name__ == '__main__':