1 # Copyright (c) 2010-2022. 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.
8 from simgrid import Engine, Actor, Comm, NetZone, Link, LinkInRoute, Mailbox, this_actor, NetworkFailureException
11 def sender(mailbox1_name: str, mailbox2_name: str) -> None:
12 mailbox1: Mailbox = Mailbox.by_name(mailbox1_name)
13 mailbox2: Mailbox = Mailbox.by_name(mailbox2_name)
15 this_actor.info(f"Initiating asynchronous send to {mailbox1.name}")
16 comm1: Comm = mailbox1.put_async(666, 5)
18 this_actor.info(f"Initiating asynchronous send to {mailbox2.name}")
19 comm2: Comm = mailbox2.put_async(666, 2)
21 this_actor.info(f"Calling wait_any..")
22 pending_comms = [comm1, comm2]
24 index = Comm.wait_any([comm1, comm2])
25 this_actor.info(f"Wait any returned index {index} (comm to {pending_comms[index].mailbox.name})")
26 except NetworkFailureException:
27 this_actor.info(f"Sender has experienced a network failure exception, so it knows that something went wrong")
28 this_actor.info(f"Now it needs to figure out which of the two comms failed by looking at their state")
30 this_actor.info(f"Comm to {comm1.mailbox.name} has state: {comm1.state_str}")
31 this_actor.info(f"Comm to {comm2.mailbox.name} has state: {comm2.state_str}")
35 except NetworkFailureException:
36 this_actor.info(f"Waiting on a FAILED comm raises an exception")
38 this_actor.info("Wait for remaining comm, just to be nice")
41 Comm.wait_any(pending_comms)
42 except Exception as e:
43 this_actor.warning(str(e))
46 def receiver(mailbox_name: str) -> None:
47 mailbox: Mailbox = Mailbox.by_name(mailbox_name)
48 this_actor.info(f"Receiver posting a receive ({mailbox_name})...")
51 this_actor.info(f"Receiver has received successfully ({mailbox_name})!")
52 except NetworkFailureException:
53 this_actor.info(f"Receiver has experience a network failure exception ({mailbox_name})")
56 def link_killer(link_name: str) -> None:
57 link_to_kill = Link.by_name(link_name)
58 this_actor.info("sleeping 10 seconds...")
59 this_actor.sleep_for(10.0)
60 this_actor.info(f"turning off link {link_to_kill.name}")
61 link_to_kill.turn_off()
62 this_actor.info("link killed. exiting")
67 zone: NetZone = NetZone.create_full_zone("AS0")
68 host1 = zone.create_host("Host1", "1f")
69 host2 = zone.create_host("Host2", "1f")
70 host3 = zone.create_host("Host3", "1f")
72 link_to_2 = LinkInRoute(zone.create_link("link_to_2", "1bps").seal())
73 link_to_3 = LinkInRoute(zone.create_link("link_to_3", "1bps").seal())
75 zone.add_route(host1.netpoint, host2.netpoint, None, None, [link_to_2], False)
76 zone.add_route(host1.netpoint, host3.netpoint, None, None, [link_to_3], False)
79 Actor.create("Sender", host1, sender, "mailbox2", "mailbox3")
80 Actor.create("Receiver-1", host2, receiver, "mailbox2").daemonize()
81 Actor.create("Receiver-2", host3, receiver, "mailbox3").daemonize()
82 Actor.create("LinkKiller", host2, link_killer, "link_to_2").daemonize()
87 if __name__ == "__main__":