Skip to content

Instantly share code, notes, and snippets.

@sheridp
Created July 12, 2017 17:44
Show Gist options
  • Save sheridp/31516e95bee350481c1a39b1597e559f to your computer and use it in GitHub Desktop.
Save sheridp/31516e95bee350481c1a39b1597e559f to your computer and use it in GitHub Desktop.
Simpy Conditional Get
env = Environment()
class ConditionalGet(simpy.resources.base.Get):
def __init__(self, resource, condition=lambda: True):
self.condition = condition
super().__init__(resource)
class aStore(simpy.resources.store.Store):
get = simpy.core.BoundClass(ConditionalGet)
def _do_get(self, event):
if event.condition():
super()._do_get(event)
q1 = aStore(env, capacity=1)
q2 = aStore(env, capacity=1)
def putter():
i = 0
while True:
yield env.timeout(5)
yield q1.put(i)
print(env.now, 'putter put %d into q1'%i)
i += 1
def mover():
while True:
yield env.timeout(20)
print(env.now, 'mover waiting to get from q1')
item = yield q1.get(lambda: len(q2.items) < q2.capacity)
print(env.now, 'mover got from q1')
print(env.now, 'mover waiting to put into q2')
yield q2.put(item)
print(env.now, 'mover put %d into q2'%item)
def getter():
while True:
yield env.timeout(50)
print(env.now, 'getter waiting to get from q2')
yield q2.get()
print(env.now, 'getter got from q2')
env.process(putter())
env.process(mover())
env.process(getter())
env.run(until=1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment