Custom Resource Types
A resource can grant its units by a rule of your own. This page shows how to write one.
What It Is
Some shops grant their units by a rule of their own, such as sharing a packer between customer classes by weight. A custom resource type replaces the rule by which a resource grants its units, and keeps the capacity that follows the registry.
How to Use It
To change how a resource grants units, subclass ResourceBase and name the class in type. ResourceBase is built on the hooks of SimPy's BaseResource, as a subclass of SimPy's Resource is. A subclass can change these:
- Override
_do_put(request)to decide when a request is granted, and_do_get(release)to decide what a release gives back. An override calls the base method, which wakeswhen_busy()andwhen_idle(), so a breakdown that counts busy time still works. - Set
PutQueueto the class of the waiting line. It can be SimPy'sSortedQueue, writtenstaticmethod(functools.partial(SortedQueue, maxlen=2)). On a store or container, atypethat sets its own queues refusesget_queueandput_queue. - Set
requesttoBoundClass(MyRequest), for a request class that subclassesDynamicResourceRequest, or setreleasethe same way, for a release class that subclassesDynamicRelease. Both base classes are indesflow.resources. A request or release class calls the base constructor, which publishes theon_changereading.
This resource keeps how long each request waited:
| docs/snippets/yaml/resources_custom.yaml:declare | |
|---|---|
The class is in shop_logic.py, beside the blueprint:
| docs/snippets/pages/custom_resources.py:builder | |
|---|---|
The class is the one the Low-level tab shows.
How It Works
DynamicResource is ResourceBase with nothing changed. The constructor is DynamicResource's, because the resource reads its capacity from the registry. A subclass that defines __init__ therefore takes *args, **kwargs and passes them on. A PutQueue of your own sets its own order and length, so it takes no queue_limit. The tests run each of these beside the same SimPy subclass in all three APIs.