python - 如何从资源商店发出优先获取请求
问题描述
简单来说,由于问题的性质,我将商店用作我的资源。
我有一些获取商店商品的请求。但是,一些获取请求具有更高的优先级,我希望它首先被处理。对于这种特殊的获取请求,我不希望遵循 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)
但是,我需要它来存储资源类,而不是存储的通用获取。
结果将是:
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
方法绑定到类,但您可以使用 a .get(priority = 1)
(或任何其他低于 10 的数字,即类中定义的基本优先PriorityGet
级)获得相同的结果。或者,您可以显式绑定该方法。
推荐阅读
- django - Django Admin - 访问新模型给出 500 错误
- algorithm - 算法检测到循环 N 次
- php - 试图显示查询并得到未定义索引的错误
- file - URLDownloadToFile 无法正确更新进度?
- c# - C# 乘法模式 for 循环。需要弄清楚如何配置每次相乘的数字表的表头
- salesforce - VisualForce 页面呈现选定联系人列表
- python-3.x - 使用字符串拆分的python中的名称格式
- python - 在 Python 上创建等值线图的问题
- python - 如何使用列表中的元素为类创建对象
- laravel - Laravel 8 上的路由定义错误。复杂的路由规则