如何从资源存储中发出优先级获取请求



简单地说,由于问题的性质,我将使用商店作为我的资源。

我收到了一些商店商品的申请。然而,有些get请求具有更高的优先级,我希望它首先得到处理。对于这种特殊的获取请求,我不希望遵循FIFO规则。

yield Store_item.get()

我试着回答这个问题。然而,我无法创建一个适合这个要求的子类。

我想要这样的东西:(但这是一个优先级资源的例子,而不是存储资源(。

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)

然而,我需要它作为存储资源类,而不是用于存储的通用get。

结果将是:

yield Store_item.priority_get()

我意识到我迟到了,但这对我来说是有效的。

首先,定义一个PriorityGet类(该代码改编自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)

然后,组装您的PriorityStore资源:

from simpy.core import BoundClass
class PriorityBaseStore(simpy.resources.store.Store):
GetQueue = simpy.resources.resource.SortedQueue
get = BoundClass(PriorityGet)

没有priority_get方法绑定到该类,但使用.get(priority = 1)(或低于10的任何其他数字,即PriorityGet类中定义的基本优先级(可以获得相同的结果。或者,您可以显式绑定该方法。

最新更新