Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Introduce a Mailbox::get_async() with no payload parameter
[simgrid.git] / examples / python / activityset-testany / activityset-testany.py
1 # Copyright (c) 2017-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 """
7 Usage: activityset-testany.py platform_file [other parameters]
8 """
9
10 import sys
11 from simgrid import Actor, ActivitySet, Engine, Comm, Exec, Io, Host, Mailbox, this_actor
12
13 def bob():
14   mbox = Mailbox.by_name("mbox")
15   disk = Host.current().get_disks()[0]
16
17   this_actor.info("Create my asynchronous activities")
18   exec = this_actor.exec_async(5e9)
19   comm = mbox.get_async()
20   io   = disk.read_async(300000000)
21
22   pending_activities = ActivitySet([exec, comm])
23   pending_activities.push(io) # Activities can be pushed after creation, too
24  
25   this_actor.info("Sleep_for a while")
26   this_actor.sleep_for(1)
27
28   this_actor.info("Test for completed activities")
29   while not pending_activities.empty():
30     completed_one = pending_activities.test_any()
31     if completed_one == None:
32       this_actor.info("Nothing matches, test again in 0.5s")
33       this_actor.sleep_for(.5)
34     elif isinstance(completed_one, Comm):
35       this_actor.info("Completed a Comm")
36     elif isinstance(completed_one, Exec):
37       this_actor.info("Completed an Exec")
38     elif isinstance(completed_one, Io):
39       this_actor.info("Completed an I/O")
40
41   this_actor.info("Last activity is complete")
42
43 def alice():
44   this_actor.info("Send 'Message'")
45   Mailbox.by_name("mbox").put("Message", 600000000)
46
47 if __name__ == '__main__':
48   e = Engine(sys.argv)
49   e.set_log_control("root.fmt:[%4.2r]%e[%5a]%e%m%n")
50
51   # Load the platform description
52   e.load_platform(sys.argv[1])
53
54   Actor.create("bob",   Host.by_name("bob"), bob)
55   Actor.create("alice", Host.by_name("alice"), alice)
56
57   e.run()