Resource - asimpy

Skip to content

# Resource

Shared resource with limited capacity.

## `Resource`

A shared resource with limited capacity.

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

    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
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    class Resource:
        """A shared resource with limited capacity."""

        def __init__(self, env: "Environment", capacity: int = 1):
            """
            Construct resource.

            Args:
                env: simulation environment.
                capacity: maximum capacity.
            """
            assert capacity > 0
            self._env = env
            self.capacity = capacity
            self._count = 0
            self._waiters = []

        async def acquire(self):
            """Acquire one unit of resource."""
            if self._count < self.capacity:
                await self._acquire_available()
            else:
                await self._acquire_unavailable()

        async def release(self):
            """Release one unit of resource."""
            self._count -= 1
            if self._waiters:
                evt = self._waiters.pop(0)
                evt.succeed()

        async def _acquire_available(self):
            def cancel():
                self._count -= 1

            self._count += 1
            evt = Event(self._env)
            evt._on_cancel = cancel
            self._env.immediate(evt.succeed)
            await evt

        async def _acquire_unavailable(self):
            evt = Event(self._env)

            def cancel():
                if evt in self._waiters:
                    self._waiters.remove(evt)

            self._waiters.append(evt)
            evt._on_cancel = cancel
            await evt
            self._count += 1

        async def __aenter__(self):
            await self.acquire()
            return self

        async def __aexit__(self, exc_type, exc, tb):
            await self.release()

### `__init__(env, capacity=1)`

Construct resource.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`env` | `Environment` | simulation environment. | required
`capacity` | `int` | maximum capacity. | `1`

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

    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    def __init__(self, env: "Environment", capacity: int = 1):
        """
        Construct resource.

        Args:
            env: simulation environment.
            capacity: maximum capacity.
        """
        assert capacity > 0
        self._env = env
        self.capacity = capacity
        self._count = 0
        self._waiters = []

### `acquire()` `async`

Acquire one unit of resource.

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

    27
    28
    29
    30
    31
    32
    async def acquire(self):
        """Acquire one unit of resource."""
        if self._count < self.capacity:
            await self._acquire_available()
        else:
            await self._acquire_unavailable()

### `release()` `async`

Release one unit of resource.

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

    34
    35
    36
    37
    38
    39
    async def release(self):
        """Release one unit of resource."""
        self._count -= 1
        if self._waiters:
            evt = self._waiters.pop(0)
            evt.succeed()