如何解决如何从FilterStore获取具有最低值针对特定属性的实体?
假设我有一些实体插入到FilterStore中。然后,我不仅要提取该实体的匹配属性(可以使用lambda
完成),还要获取另一个属性的最小值。
from random import random
import simpy
def main():
env = simpy.Environment()
queue = simpy.FilterStore(env)
env.process(inserting(env,queue))
env.process(removing(env,queue))
env.run(until=100)
def inserting(env,queue):
while True:
entity = {'foo': 'bar','val': int(random()*10)}
queue.put(entity)
print(f"{round(env.Now,2)}>> PUT {entity}")
yield env.timeout(4)
entity = {'foo': 'baz',2)}>> PUT {entity}")
yield env.timeout(4)
def removing(env,queue):
yield env.timeout(30)
while True:
entity = yield queue.get(lambda e: e['foo'] == 'bar') # how to filter for min(e['val'])?
print(f"{round(env.Now,2)}>> GOT {entity}")
yield env.timeout(5)
if __name__ == '__main__':
main()
我可以做的一件事就是访问queue.items
,“手动”获得最小值,但是如果我尝试获得该值,队列却空了怎么办?在插入新实体之前,我如何让我的流程保持检查状态?在我的示例中,我要做的是在再次获取一个实体之前强迫再等待一个timeout
,但是我想知道是否存在一种更聪明的(本机)方式,而无需设置“先验”超时时间
def removing_manual(env,queue):
while True:
minimum = float('inf')
for entity in queue.items:
if minimum > entity['val'] and entity['foo'] == 'bar':
minimum = entity['val']
if minimum == float('inf'):
yield env.timeout(1)
continue
entity = yield queue.get(lambda e: e['foo'] == 'bar' and e['val'] == minimum)
print(f"{round(env.Now,2)}>> GOT {entity}")
yield env.timeout(5)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。