Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / python / comm-suspend / comm-suspend.py
1 # Copyright (c) 2010-2023. 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 suspend and resume an asynchronous communication.
7 """
8
9 from argparse import ArgumentParser
10 import sys
11
12 from simgrid import Actor, Comm, Engine, Mailbox, this_actor
13
14
15 def create_parser() -> ArgumentParser:
16     parser = ArgumentParser()
17     parser.add_argument(
18         '--platform',
19         type=str,
20         required=True,
21         help='path to the platform description'
22     )
23     return parser
24
25
26 def sender():
27     mailbox: Mailbox = Mailbox.by_name("receiver")
28     payload = "Sent message"
29
30     # Create a communication representing the ongoing communication and then
31     simulated_size_in_bytes = 13194230
32     comm: Comm = mailbox.put_init(payload, simulated_size_in_bytes)
33     this_actor.info(f"Suspend the communication before it starts (remaining: {comm.remaining:.0f} bytes)"
34                     f" and wait a second.")
35     this_actor.sleep_for(1)
36     this_actor.info(f"Now, start the communication (remaining: {comm.remaining:.0f} bytes) and wait another second.")
37     comm.start()
38     this_actor.sleep_for(1)
39     this_actor.info(f"There is still {comm.remaining:.0f} bytes to transfer in this communication."
40                     " Suspend it for one second.")
41     comm.suspend()
42     this_actor.info(f"Now there is {comm.remaining:.0f} bytes to transfer. Resume it and wait for its completion.")
43     comm.resume()
44     comm.wait()
45     this_actor.info(f"There is {comm.remaining:.0f} bytes to transfer after the communication completion.")
46     this_actor.info(f"Suspending a completed activity is a no-op.")
47     comm.suspend()
48
49
50 def receiver():
51     mailbox: Mailbox = Mailbox.by_name("receiver")
52     this_actor.info("Wait for the message.")
53     received: str = mailbox.get()
54     this_actor.info(f"I got '{received}'.")
55
56
57 def main():
58     settings = create_parser().parse_known_args()[0]
59     e = Engine(sys.argv)
60     e.load_platform(settings.platform)
61     Actor.create("sender", e.host_by_name("Tremblay"), sender)
62     Actor.create("receiver", e.host_by_name("Jupiter"), receiver)
63     e.run()
64
65
66 if __name__ == "__main__":
67     main()