Skip to content

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

  1. AllOf(env, **events) is itself an Event that triggers only when every named child event has triggered.

  2. Its value is a dict mapping each keyword to the corresponding child event's value. Timeout events resolve to None, so all three values here are None.

  3. The coordinator resumes at t=5 because that is when the last child (gamma, duration 5) completes. The faster children (beta at t=1, alpha at t=3) have no visible effect until gamma finishes.

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?