Environment - asimpy

Skip to content

# Environment

Simulation environment.

## `Environment`

Simulation environment.

Source code in `src/asimpy/environment.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
    40
    41
    42
    class Environment:
        """Simulation environment."""

        def __init__(self):
            self._now = 0
            self._pending = []

        @property
        def now(self):
            return self._now

        def immediate(self, callback):
            self.schedule(self._now, callback)

        def schedule(self, time, callback):
            heapq.heappush(self._pending, _Pending(time, callback))

        def timeout(self, delay):
            return Timeout(self, delay)

        def run(self, until=None):
            while self._pending:
                pending = heapq.heappop(self._pending)
                if until is not None and pending.time > until:
                    break
                result = pending.callback()
                if (result is not NO_TIME) and (pending.time > self._now):
                    self._now = pending.time

        def __str__(self):
            return f"Env(t={self._now})"