Firstof - asimpy

Skip to content

# Firstof

Wait for the first of a set of events.

## `FirstOf`

Bases: `Event`

Wait for the first of a set of events.

Source code in `src/asimpy/firstof.py`

     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    class FirstOf(Event):
        """Wait for the first of a set of events."""

        def __init__(self, env: Environment, **events: Any):
            """
            Construct new collective wait.

            Args:
                env: simulation environment.
                events: name=thing items to wait for.

            Raises:
                ValueError: if no events provided.

            Example:

            ```
            name, value = await FirstOf(env, a=q1.get(), b=q2.get())
            ```
            """
            if not events:
                raise ValueError("FirstOf requires at least one event")
            super().__init__(env)

            self._done = False
            self._events = {}

            for key, obj in events.items():
                evt = _ensure_event(env, obj)
                self._events[key] = evt
                evt._add_waiter(lambda v, k=key, e=evt: self._child_done(k, v, e))

        def _child_done(self, key, value, winner):
            if self._done:
                return

            self._done = True

            for evt in self._events.values():
                if evt is not winner:
                    evt.cancel()

            self.succeed((key, value))

### `__init__(env, **events)`

Construct new collective wait.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`env` | `Environment` | simulation environment. | required
`events` | `Any` | name=thing items to wait for. | `{}`

Raises:

Type | Description
--- | ---
`ValueError` | if no events provided.

Example:

    name, value = await FirstOf(env, a=q1.get(), b=q2.get())

Source code in `src/asimpy/firstof.py`

    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    def __init__(self, env: Environment, **events: Any):
        """
        Construct new collective wait.

        Args:
            env: simulation environment.
            events: name=thing items to wait for.

        Raises:
            ValueError: if no events provided.

        Example:

        ```
        name, value = await FirstOf(env, a=q1.get(), b=q2.get())
        ```
        """
        if not events:
            raise ValueError("FirstOf requires at least one event")
        super().__init__(env)

        self._done = False
        self._events = {}

        for key, obj in events.items():
            evt = _ensure_event(env, obj)
            self._events[key] = evt
            evt._add_waiter(lambda v, k=key, e=evt: self._child_done(k, v, e))