Is there a way to terminate the simpy simulation by using a command like env.exit()? I don't understand how to place an event into env.run(until=event)
. I want to terminate the simulation when there are no objects left in my certain Simpy Stores
. How can I do that?
Asked
Active
Viewed 837 times
2

RookieScientist
- 314
- 2
- 12
2 Answers
2
Everything is an event in simpy, even the environment itself. Thus, you can terminate the simulation marking as succeed the "root" event.
# Save the event somewhere
end_event = env.event()
# Later, when you want to terminate the simulation, run
end_event.succeed()
In order to check if a store is empty, just check if its items
len is equal to zero.
If you put all together, you can do something like that to solve your problem:
store = simpy.FilterStore(env, capacity=10)
if len(store.items) == 0:
end_event.succeed()

Andrea
- 4,262
- 4
- 37
- 56
-
You are welcome @kerim, I am glad I have helped you – Andrea Feb 24 '21 at 11:03
1
So the whole example could be:
env = simpy.Environment()
end_envt = env.event()
def process(store):
yield store.get()
if len(store.items) == 0:
end_event.succeed()
store = simpy.FilterStore(env, capacity=10)
env.process(process(store))
env.run(until= end_envet)