Event - asimpy

Skip to content

# Event

Events.

## `Event`

Manage an event.

Source code in `src/asimpy/event.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
    52
    53
    54
    55
    56
    57
    58
    class Event:
        """Manage an event."""

        def __init__(self, env: "Environment"):
            """
            Construct a new event.

            Args:
                env: simulation environment.
            """
            self._env = env
            self._triggered = False
            self._cancelled = False
            self._value = None
            self._waiters = []
            self._on_cancel = None

        def succeed(self, value: Any = None):
            """
            Handle case of event succeeding.

            Args:
                value: value associated with event.
            """
            if self._triggered or self._cancelled:
                return
            self._triggered = True
            self._value = value
            for callback in self._waiters:
                callback(value)
            self._waiters.clear()

        def cancel(self):
            """Cancel event."""
            if self._triggered or self._cancelled:
                return
            self._cancelled = True
            self._waiters.clear()
            if self._on_cancel:
                self._on_cancel()

        def _add_waiter(self, callback):
            if self._triggered:
                callback(self._value)
            elif not self._cancelled:
                self._waiters.append(callback)

        def __await__(self):
            value = yield self
            return value

### `__init__(env)`

Construct a new event.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`env` | `Environment` | simulation environment. | required

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

    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    def __init__(self, env: "Environment"):
        """
        Construct a new event.

        Args:
            env: simulation environment.
        """
        self._env = env
        self._triggered = False
        self._cancelled = False
        self._value = None
        self._waiters = []
        self._on_cancel = None

### `cancel()`

Cancel event.

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

    41
    42
    43
    44
    45
    46
    47
    48
    def cancel(self):
        """Cancel event."""
        if self._triggered or self._cancelled:
            return
        self._cancelled = True
        self._waiters.clear()
        if self._on_cancel:
            self._on_cancel()

### `succeed(value=None)`

Handle case of event succeeding.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`value` | `Any` | value associated with event. | `None`

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

    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    def succeed(self, value: Any = None):
        """
        Handle case of event succeeding.

        Args:
            value: value associated with event.
        """
        if self._triggered or self._cancelled:
            return
        self._triggered = True
        self._value = value
        for callback in self._waiters:
            callback(value)
        self._waiters.clear()