Search code examples
python-2.7simpy

disambiguating simpy multi event results


I am trying to code a simple multiplexer in simpy as part of a network modelling exercise. What I have is two stores, s1, and s2 and I wish to do a single yield which waits for one or both of s1 and s2 to return a 'packet' via the standard Store.get() method. This does work, but I am unable then to determine which of the two stores actually returned the packet. What is the right way to do this - by inserting the appropriate code instead of the comment in the code below?

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)

Solution

  • You need to bind the Store.get() event to a name and then check if it is in the results, e.g.:

    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