simpy.resources.base — Base classes for all resources

This module contains the base classes for Simpy’s resource system.

BaseResource defines the abstract base resource. The request for putting something into or getting something out of a resource is modeled as an event that has to be yielded by the requesting process. Put and Get are the base event types for this.

class simpy.resources.base.BaseResource(env)

This is the abstract base class for all SimPy resources.

All resources are bound to a specific Environment env.

You can put() something into the resources or get() something out of it. Both methods return an event that the requesting process has to yield.

If a put or get operation can be performed immediately (because the resource is not full (put) or not empty (get)), that event is triggered immediately.

If a resources is too full or too empty to perform a put or get request, the event is pushed to the put_queue or get_queue. An event is popped from one of these queues and triggered as soon as the corresponding operation is possible.

put() and get() only provide the user API and the general framework and should not be overridden in subclasses. The actual behavior for what happens when a put/get succeeds should rather be implemented in _do_put() and _do_get().

PutQueue

The type to be used for the put_queue. This can either be a plain list (default) or a subclass of it.

alias of list

GetQueue

The type to be used for the get_queue. This can either be a plain list (default) or a subclass of it.

alias of list

put_queue = None

Queue/list of events waiting to get something out of the resource.

get_queue = None

Queue/list of events waiting to put something into the resource.

put

Create a new Put event.

alias of Put

get

Create a new Get event.

alias of Get

_do_put(event)

Actually perform the put operation.

This methods needs to be implemented by subclasses. It receives the put_event that is created at each request and doesn’t need to return anything.

_trigger_put(get_event)

Trigger pending put events after a get event has been executed.

_do_get(event)

Actually perform the get operation.

This methods needs to be implemented by subclasses. It receives the get_event that is created at each request and doesn’t need to return anything.

_trigger_get(put_event)

Trigger pending get events after a put event has been executed.

class simpy.resources.base.Put(resource)

The base class for all put events.

It receives the resource that created the event.

This event (and all of its subclasses) can act as context manager and can be used with the with statement to automatically cancel a put request if an exception or an simpy.events.Interrupt occurs:

with res.put(item) as request:
    yield request

It is not used directly by any resource, but rather sub-classed for each type.

cancel(exc_type, exc_value, traceback)

Cancel the current put request.

This method has to be called if a process received an Interrupt or an exception while yielding this event and is not going to yield this event again.

If the event was created in a with statement, this method is called automatically.

class simpy.resources.base.Get(resource)

The base class for all get events.

It receives the resource that created the event.

This event (and all of its subclasses) can act as context manager and can be used with the with statement to automatically cancel a get request if an exception or an simpy.events.Interrupt occurs:

with res.get() as request:
    yield request

It is not used directly by any resource, but rather sub-classed for each type.

cancel(exc_type, exc_value, traceback)

Cancel the current get request.

This method has to be called if a process received an Interrupt or an exception while yielding this event and is not going to yield this event again.

If the event was created in a with statement, this method is called automatically.