1 from argparse import ArgumentParser
2 from dataclasses import dataclass
5 from simgrid import Actor, Engine, Host, Mutex, this_actor
8 def create_parser() -> ArgumentParser:
9 parser = ArgumentParser()
14 help='path to the platform description'
20 help='how many pairs of actors should be started?'
30 def worker_context_manager(mutex: Mutex, result: ResultHolder):
31 """ Worker that uses a context manager to acquire/release the shared mutex
32 :param mutex: Shared mutex that guards read/write access to the shared result
33 :param result: Shared result which will be updated by the worker
35 this_actor.info(f"I just started")
37 this_actor.info(f"acquired the mutex with context manager")
39 this_actor.info(f"updated shared result, which is now {result.value}")
40 this_actor.info(f"released the mutex after leaving the context manager")
41 this_actor.info("Bye now!")
44 def worker(mutex: Mutex, result: ResultHolder):
45 """ Worker that manually acquires/releases the shared mutex
46 :param mutex: Shared mutex that guards read/write access to the shared result
47 :param result: Shared result which will be updated by the worker
49 this_actor.info(f"I just started")
51 this_actor.info(f"acquired the mutex manually")
53 this_actor.info(f"updated shared result, which is now {result.value}")
55 this_actor.info(f"released the mutex manually")
56 this_actor.info("Bye now!")
60 """ Spawns `--workers` workers and wait until they have all updated the shared result, then displays it before
61 leaving. Alternatively spawns `worker_context_manager()` and `worker()` workers.
62 :param settings: Simulation settings
64 result = ResultHolder(value=0)
66 for i in range(settings.actors):
67 Actor.create(f"worker-{i}(mgr)", Host.by_name("Jupiter"), worker_context_manager, mutex, result)
68 Actor.create(f"worker-{i}", Host.by_name("Tremblay"), worker, mutex, result)
69 this_actor.sleep_for(10)
70 this_actor.info(f"The final result is: {result.value}")
74 settings = create_parser().parse_known_args()[0]
76 e.load_platform(settings.platform)
77 Actor.create("master", Host.by_name("Tremblay"), master, settings)
81 if __name__ == "__main__":