simpy.resources.resource – Resource type resources

This module contains all Resource like resources.

These resources can be used by a limited number of processes at a time (e.g., a gas station with a limited number of fuel pumps). Processes request these resources to become a user (or to own them) and have to release them once they are done (e.g., vehicles arrive at the gas station, use a fuel-pump, if one is available, and leave when they are done).

Requesting a resources is modeled as “putting a process’ token into the resources” and releasing a resources correspondingly as “getting a process’ token out of the resource”. Thus, calling request()/release() is equivalent to calling put()/get(). Note, that releasing a resource will always succeed immediately, no matter if a process is actually using a resource or not.

Beside Resource, there are a PriorityResource, were processes can define a request priority, and a PreemptiveResource whose resource users can be preempted by other processes with a higher priority.

class simpy.resources.resource.Resource(env, capacity=1)

A resource has a limited number of slots that can be requested by a process.

If all slots are taken, requesters are put into a queue. If a process releases a slot, the next process is popped from the queue and gets one slot.

The env parameter is the Environment instance the resource is bound to.

The capacity defines the number of slots and must be a positive integer.

users = None

List of Request events for the processes that are currently using the resource.

queue = None

Queue/list of pending Request events that represent processes waiting to use the resource.

capacity

Maximum capacity of the resource.

count

Number of users currently using the resource.

request

Create a new Request event.

alias of Request

release

Create a new Release event.

alias of Release

class simpy.resources.resource.PriorityResource(env, capacity=1)

This class works like Resource, but requests are sorted by priority.

The queue is kept sorted by priority in ascending order (a lower value for priority results in a higher priority), so more important request will get the resource earlier.

PutQueue

The type to be used for the put_queue.

alias of SortedQueue

GetQueue

The type to be used for the get_queue.

alias of list

request

Create a new PriorityRequest event.

alias of PriorityRequest

class simpy.resources.resource.PreemptiveResource(env, capacity=1)

This resource mostly works like Resource, but users of the resource can be preempted by higher prioritized requests.

Furthermore, the queue of requests is also sorted by priority.

If a less important request is preempted, the process of that request will receive an Interrupt with a Preempted instance as cause.

class simpy.resources.resource.Preempted(by, usage_since)
by = None

The preempting simpy.events.Process.

usage_since = None

The simulation time at which the preempted process started to use the resource.

class simpy.resources.resource.Request(resource)

Request access on the resource. The event is triggered once access is granted.

If the maximum capacity of users is not reached, the requesting process obtains the resource immediately. If the maximum capacity is reached, the requesting process waits until another process releases the resource.

The request is automatically released when the request was created within a with statement.

class simpy.resources.resource.Release(resource, request)

Releases the access privilege to resource granted by request. This event is triggered immediately.

If there’s another process waiting for the resource, resume it.

If the request was made in a with statement (e.g., with res.request() as req:), this method is automatically called when the with block is left.

request = None

The request (Request) that is to be released.

class simpy.resources.resource.PriorityRequest(resource, priority=0, preempt=True)

Request the resource with a given priority. If the resource supports preemption and preempted is true other processes with access to the resource may be preempted (see PreemptiveResource for details).

This event type inherits Request and adds some additional attributes needed by PriorityResource and PreemptiveResource

priority = None

The priority of this request. A smaller number means higher priority.

preempt = None

Indicates whether the request should preempt a resource user or not (this flag is not taken into account by PriorityResource).

time = None

The time at which the request was made.

key = None

Key for sorting events. Consists of the priority (lower value is more important) and the time at witch the request was made (earlier requests are more important).

class simpy.resources.resource.SortedQueue(maxlen=None)

Queue that sorts events by their key attribute.

maxlen = None

Maximum length of the queue.

append(item)

Append item to the queue and keep the queue sorted.

Raise a RuntimeError if the queue is full.