store.get()
request on every loop iteration. If the timeout happened first, the get
request was abandoned. Kind of like a customer leaving a queue every second, and then rejoining at the back of the queue.job = store.get()
request once, before the main loop starts.job
request remains active.job = store.get()
request to wait for the next item.Harry Munro CEng
Independent Simulation Consultant, Aspegio Ltd
Founder, School of Simulation
LinkedIn: https://www.linkedin.com/in/harryjmunro/
--
You received this message because you are subscribed to the Google Groups "python-simpy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-simpy...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/python-simpy/5af7497e-63c9-4218-ac89-332e123322d1n%40googlegroups.com.
Hello Faiz. I prompted your code and got this refactored code back. Not sure if it covers your needs.
Please comment.
Cesar
import simpy
def consumer(env, store, num_jobs=10, timeout_duration=1):
"""Consumes jobs from the store or times out if none are available."""
for i in range(num_jobs):
job_event = store.get()
timeout_event = env.timeout(timeout_duration, value=i)
result = yield env.any_of([job_event, timeout_event])
if job_event in result:
print(f"{env.now:.2f}: got job {job_event.value}")
if timeout_event in result:
print(f"{env.now:.2f}: timeout {timeout_event.value}")
def producer(env, store, num_jobs=10, interval=1):
"""Produces jobs and puts them into the store at regular intervals."""
for i in range(num_jobs):
print(f"{env.now:.2f}: putting job {i}")
yield env.timeout(interval)
yield store.put({i: i})
def main():
env = simpy.Environment()
store = simpy.Store(env)
env.process(producer(env, store))
env.process(consumer(env, store))
env.run()
if __name__ == "__main__":
main()
--