pythonsimulationsimpy

How to terminate simulation in SimPy 4


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?


Solution

  • 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()