Skip to content

Resource Example

pydes.Resource(sim, capacity=1)

Resources can be requested and released by components and therefore are really useful in modeling real world scenarios quere components must be shared among different processess.

Parameters:

Name Type Description Default
sim Simulator

The simulator instance.

required
capacity int

The capacity of the resource, default is 1.

1

Methods:

Name Description
request

tries to get the ownership of this Resource and waits if the resource is not avialable.

release

gives back the ownership of the Resource so that other user can make use of it.

request(by)

Request the resource.

If the resource is idle, the component can acquire it. Otherwise, it waits until the resource becomes idle.

Parameters:

Name Type Description Default
by Component

The component requesting the resource.

required

release(by)

Release the resource.

Parameters:

Name Type Description Default
by Component

The component releasing the resource.

required

Raises:

Type Description
PydesError

If the component has not previously requested the resource.

usage()

Get the current usage of the resource.

capacity()

Get the capacity of the resource.

is_idle()

Check if the resource is idle.

Imports

from pydes import Simulator, Component, Resource

Define Model

class Process1(Component):
    def __init__(self, sim: Simulator, resource: Resource):
        self.sim = sim
        self.resource = resource

    def main(self):
        self.sim.record(self.id, "requesting resource")
        self.resource.request(self)
        self.sim.record(self.id, "resource aquired")
        self.sim.sleep(10)
        self.sim.record(self.id, "releasing resource")
        self.resource.release(self)
        self.sim.record(self.id, "resource released")
class Process2(Component):
    def __init__(self, sim: Simulator, resource: Resource):
        self.sim = sim
        self.resource = resource

    def main(self):
        self.sim.record(self.id, "sleeps before requesting")
        self.sim.sleep(5)
        self.sim.record(self.id, "requesting resource")
        self.resource.request(self)
        self.sim.record(self.id, "resource aquired")
        self.sim.sleep(10)
        self.sim.record(self.id, "releasing resource")
        self.resource.release(self)
        self.sim.record(self.id, "resource released")

Run Simulation

sim = Simulator()
resource = Resource(sim)
p1 = Process1(sim, resource)
p2 = Process2(sim, resource)
sim.schedule(p1.main)
sim.schedule(p2.main)
sim.run()
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time                           | component       | value                                    | description                    |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
|                                |                 |                                          |                                |
| 0                              | Process1.0      | requesting resource                      | None                           |
| 0                              | Process1.0      | resource aquired                         | None                           |
| 0                              | Process2.0      | sleeps before requesting                 | None                           |
| 5                              | Process2.0      | requesting resource                      | None                           |
| 10                             | Process1.0      | releasing resource                       | None                           |
| 10                             | Process1.0      | resource released                        | None                           |
| 10                             | Process2.0      | resource aquired                         | None                           |
| 20                             | Process2.0      | releasing resource                       | None                           |
| 20                             | Process2.0      | resource released                        | None                           |