简体   繁体   中英

How do i make a priority get request from resource store

In simpy, I'm using the store as my resource due to the nature of the problem.

I have a few get request for a store item. However, some get request have higher priority and I wish for it to be processed first. I do not wish to follow the FIFO rules for such special get request.

yield Store_item.get()

I tried following this question . However, I'm unable to create a subclass that suits this requirement.

I want something like this:(but this is an example of priority resource and not store resource).

def resource_user(name, env, resource, wait, prio):
    yield env.timeout(wait)
    with resource.request(priority=prio) as req:
         print('%s requesting at %s with priority=%s'% (name,env.now,prio))
         yield req
         print('%s got resource at %s' % (name, env.now))
         yield env.timeout(3)

However, I need it for store resource class and not a generic get for store.

The outcome will be:

yield Store_item.priority_get()

I realize I'm late, but this is what worked for me.

First, define a PriorityGet class (this code was adapted from the source of simpy):

class PriorityGet(simpy.resources.base.Get):

    def __init__(self, resource, priority=10, preempt=True):
        self.priority = priority
        """The priority of this request. A smaller number means higher
        priority."""

        self.preempt = preempt
        """Indicates whether the request should preempt a resource user or not
        (:class:`PriorityResource` ignores this flag)."""

        self.time = resource._env.now
        """The time at which the request was made."""

        self.usage_since = None
        """The time at which the request succeeded."""

        self.key = (self.priority, self.time, not self.preempt)
        """Key for sorting events. Consists of the priority (lower value is
        more important), the time at which the request was made (earlier
        requests are more important) and finally the preemption flag (preempt
        requests are more important)."""

        super().__init__(resource)

Then, assemble your PriorityStore resource:

from simpy.core import BoundClass

class PriorityBaseStore(simpy.resources.store.Store):

    GetQueue = simpy.resources.resource.SortedQueue

    get = BoundClass(PriorityGet)

No priority_get method is bound to the class, but you can achieve the same result with a .get(priority = 1) (or any other number lower than 10, the base priority defined in the PriorityGet class). Alternatively, you can bound the method explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM