Python 2.7 消除simpy多事件结果的歧义
作为网络建模练习的一部分,我正在尝试用simpy编写一个简单的多路复用器。我有两个存储区,s1和s2,我想做一个单一的输出,等待s1和s2中的一个或两个通过标准的Store.get()方法返回“packet”。这确实有效,但我无法确定两个存储中的哪一个实际返回了数据包。正确的方法是什么?插入适当的代码而不是下面代码中的注释Python 2.7 消除simpy多事件结果的歧义,python-2.7,simpy,Python 2.7,Simpy,作为网络建模练习的一部分,我正在尝试用simpy编写一个简单的多路复用器。我有两个存储区,s1和s2,我想做一个单一的输出,等待s1和s2中的一个或两个通过标准的Store.get()方法返回“packet”。这确实有效,但我无法确定两个存储中的哪一个实际返回了数据包。正确的方法是什么?插入适当的代码而不是下面代码中的注释 import simpy env = simpy.Environment() s1 = simpy.Store(env, capacity=4) s2 = simpy.Sto
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