How to terminate simulation in SimPy 4

Question:

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 By: RookieScientist

||

Answers:

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()
Answered By: Andrea

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)
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.