Skip to content

Resource

Shared resource with limited capacity.

Resource

Shared resource with limited capacity.

Source code in src/asimpy/resource.py
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
class Resource:
    """Shared resource with limited capacity."""

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

        Args:
            env: simulation environment.
            capacity: maximum simultaneous users.
        """
        self._env = env
        self._capacity = capacity
        self._count = 0
        self._waiting = []

    async def acquire(self):
        """Acquire one unit of the resource."""
        await _Acquire(self)

    async def release(self):
        """Release one unit of the resource."""
        await _Release(self)

    async def __aenter__(self):
        """Acquire one unit of the resource using `async with`."""
        await self.acquire()
        return self

    async def __aexit__(self, exc_type, exc, tb):
        """Release one unit of the resource acquired with `async with`."""
        await self.release()

__aenter__() async

Acquire one unit of the resource using async with.

Source code in src/asimpy/resource.py
35
36
37
38
async def __aenter__(self):
    """Acquire one unit of the resource using `async with`."""
    await self.acquire()
    return self

__aexit__(exc_type, exc, tb) async

Release one unit of the resource acquired with async with.

Source code in src/asimpy/resource.py
40
41
42
async def __aexit__(self, exc_type, exc, tb):
    """Release one unit of the resource acquired with `async with`."""
    await self.release()

__init__(env, capacity=1)

Create a new resource.

Parameters:

Name Type Description Default
env Environment

simulation environment.

required
capacity int

maximum simultaneous users.

1
Source code in src/asimpy/resource.py
14
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, env: "Environment", capacity: int = 1):
    """
    Create a new resource.

    Args:
        env: simulation environment.
        capacity: maximum simultaneous users.
    """
    self._env = env
    self._capacity = capacity
    self._count = 0
    self._waiting = []

acquire() async

Acquire one unit of the resource.

Source code in src/asimpy/resource.py
27
28
29
async def acquire(self):
    """Acquire one unit of the resource."""
    await _Acquire(self)

release() async

Release one unit of the resource.

Source code in src/asimpy/resource.py
31
32
33
async def release(self):
    """Release one unit of the resource."""
    await _Release(self)