SimPy Environment Explained: The Heart of Every Simulation
The SimPy Environment is the least glamorous and most important part of every simulation. It's the clock, the scheduler, and the coordinator all in one.
What Is the Environment?
The environment does three things:
- Tracks time -
env.nowtells you the current simulation time - Schedules events - Knows what happens next and when
- Runs the simulation - Advances time from event to event
Every SimPy simulation starts the same way:
import simpy
env = simpy.Environment()
Time in SimPy
The environment tracks simulation time, not wall-clock time.
env = simpy.Environment()
print(env.now) # 0
Time starts at zero. It only advances when you run the simulation and events occur.
The units are whatever you decide—seconds, minutes, hours, days. SimPy doesn't care. Just be consistent.
Creating Processes
The environment registers processes—the things that happen in your simulation:
def my_process(env):
print(f"Starting at {env.now}")
yield env.timeout(5)
print(f"Finishing at {env.now}")
env.process(my_process(env))
env.process() takes a generator and adds it to the event queue. Nothing runs until you call env.run().
Running the Simulation
Three ways to run:
# Run until all processes complete
env.run()
# Run until a specific time
env.run(until=100)
# Run until a specific event
env.run(until=some_event)
When you call run(), the environment:
1. Picks the next event from the queue
2. Advances time to that event
3. Executes the event
4. Repeats until done
Starting Time Not at Zero
Sometimes you want the simulation to start at a different time:
env = simpy.Environment(initial_time=100)
print(env.now) # 100
Useful for warm-up periods or continuing from a previous state.
Peeking at the Future
You can check what's scheduled without advancing time:
print(env.peek()) # Time of next scheduled event
Returns infinity if nothing is scheduled.
The Event Queue
Under the hood, the environment maintains a priority queue of events. Each event has a time and a callback.
You rarely interact with this directly. Processes, timeouts, and resources all create events for you. The environment just runs them in order.
Common Patterns
Passing the Environment
Every process needs the environment:
def customer(env, name):
print(f"{name} arrives at {env.now}")
yield env.timeout(5)
print(f"{name} leaves at {env.now}")
env = simpy.Environment()
env.process(customer(env, "Alice"))
env.run()
Running Multiple Processes
env = simpy.Environment()
env.process(customer(env, "Alice"))
env.process(customer(env, "Bob"))
env.process(customer(env, "Charlie"))
env.run()
All three start at time 0 and run concurrently (in simulation terms).
Accessing Time Mid-Simulation
def process(env):
start = env.now
yield env.timeout(10)
elapsed = env.now - start
print(f"Elapsed time: {elapsed}")
env.now is always the current simulation time.
Environment vs RealTimeEnvironment
SimPy also has RealtimeEnvironment for real-time simulations:
env = simpy.rt.RealtimeEnvironment(factor=1.0)
With factor=1.0, one simulation second equals one real second. Useful for interfacing with external systems or creating live demonstrations.
With factor=0.5, the simulation runs at half speed. With factor=2.0, double speed.
Gotchas
Forgetting to Run
env = simpy.Environment()
env.process(my_process(env))
# Forgot env.run() - nothing happens!
Checking Time Too Early
env = simpy.Environment()
print(env.now) # 0 - always, until you run
Time only advances during env.run().
Using Real Time Functions
import time
def bad_process(env):
time.sleep(5) # Don't do this!
yield env.timeout(0)
time.sleep() pauses your Python program, not the simulation. Use env.timeout() instead.
Summary
The environment is:
- The simulation clock (env.now)
- The process registry (env.process())
- The execution engine (env.run())
Not the most exciting component. But without it, nothing else works.
Next Steps
Discover the Power of Simulation
Want to become a go-to expert in simulation with Python? The Complete Simulation Bootcamp will show you how simulation can transform your career and your projects.
Explore the Bootcamp