我正在尝试将简单的多路复用器编写为简单的多路复用器,作为网络建模练习的一部分。我有两个商店,s1和s2,我希望做一个单一的产量,等待s1和s2中的一个或两个通过标准的Store.get()方法返回'数据包'。这确实有效,但我无法确定两个商店中哪个实际返回了数据包。正确的方法是什么 - 通过在下面的代码中插入适当的代码而不是注释?
import simpy
env = simpy.Environment()
s1 = simpy.Store(env, capacity=4)
s2 = simpy.Store(env, capacity=4)
def putpkts():
a =1
b= 2
s1.put(a)
s2.put(b)
yield env.timeout(40)
s1.put(a)
yield env.timeout(40)
s2.put(b)
yield env.timeout(40)
def getpkts():
while True:
stuff = (yield s1.get() | s2.get() )
# here, I need to put code to determine
# whether the 'stuff' dict
# contains an item from store s1, or store s2, or both.
# how can I do this?
proc1 = env.process(putpkts())
proc2 = env.process(getpkts())
env.run(until = proc2)
答案 0 :(得分:1)
您需要将Store.get()
事件绑定到名称,然后检查它是否在结果中,例如:
get1 = s1.get()
get2 = s2.get()
results = yield get1 | get2
item1 = results[get1] if get1 in results else None
item2 = results[get2] if get2 in results else None