消除简单多事件结果的歧义

disambiguating simpy multi event results

我正在尝试在 simpy 中编写一个简单的多路复用器,作为网络建模练习的一部分。我有两家商店,s1 和 s2,我希望通过标准 Store.get( ) 方法。这确实有效,但我无法确定 两家商店中的哪一家 实际上 return 发送了数据包。执行此操作的正确方法是什么 - 通过在下面的代码中插入适当的代码而不是注释?

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)

您需要将 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