AllOf
Source and Output
"""Example: AllOf waits for all named events to complete."""
from asimpy import AllOf, Environment, Process
from _util import example
# Simulated durations for three parallel tasks.
TASK_DURATIONS = {"alpha": 3, "beta": 1, "gamma": 5}
class Coordinator(Process):
async def run(self):
self._env.log("coordinator", "launch tasks")
tasks = {name: self.timeout(dur) for name, dur in TASK_DURATIONS.items()}
results = await AllOf(self._env, **tasks)
self._env.log("coordinator", f"all done at keys={sorted(results)}")
def main():
env = Environment()
Coordinator(env)
env.run()
return env
if __name__ == "__main__":
example(main)
| time | name | event |
|---|---|---|
| 0 | coordinator | launch tasks |
| 5 | coordinator | all done at keys=['alpha', 'beta', 'gamma'] |
Key Points
-
AllOf(env, **events)is itself anEventthat triggers only when every named child event has triggered. -
Its value is a
dictmapping each keyword to the corresponding child event's value.Timeoutevents resolve toNone, so all three values here areNone. -
The coordinator resumes at t=5 because that is when the last child (
gamma, duration 5) completes. The faster children (betaat t=1,alphaat t=3) have no visible effect untilgammafinishes.
Check for Understanding
If you added a fourth task with duration 0, would the coordinator still see
all four keys in the result dict?
At what simulation time would the coordinator resume?