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

标签 python simpy

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

我有一些对商店商品的获取请求。但是,某些 get 请求具有更高的优先级,我希望首先处理它。对于这种特殊的获取请求,我不希望遵循 FIFO 规则。

yield Store_item.get()

我尝试关注this question 。但是,我无法创建适合此要求的子类。

我想要这样的东西:(但这是优先资源的示例,而不是存储资源)。

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)

但是,我需要它用于商店资源类,而不是商店的通用获取。

结果将是:

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 方法绑定(bind)到该类,但您可以使用 .get(priority = 1)(或任何其他低于 10 的数字)获得相同的结果,在 PriorityGet 类中定义的基本优先级)。或者,您可以显式绑定(bind)该方法。

关于python - 如何从资源存储中发出优先获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603000/

相关文章:

python - 请求 Simpy 资源永远不会成功

python - 在Python中使用twisted以异步模式发送数据

python - 如何从python中的多个文件夹中读取多个图像

python - 如何从 BeautifulSoup 的网页获取文件的大小

python - 来自 python 的数据显示在一列而不是 ppostgresqll 上的两列

python - 'builtin_function_or_method' 对象没有属性 'randrange'

python - django在static子文件夹中找不到静态文件

python - 需要 SimPy 模拟可用性方面的帮助

python - 简单地说明如何多次运行模拟

Python + simPy : name 'move' is not defined