Your First SimPy Simulation (Hello World for Discrete Event Simulation)
Every programming journey starts with "Hello World." Simulation is no different—except instead of printing text, we're modelling events over time.
Let's build your first simulation in under five minutes.
The Simplest Possible Simulation
import simpy
def say_hello(env):
yield env.timeout(1)
print(f"Hello at time {env.now}")
env = simpy.Environment()
env.process(say_hello(env))
env.run()
Output:
Hello at time 1
That's it. You've simulated an event happening after one unit of time.
Breaking It Down
Let's understand what each piece does.
The Environment
env = simpy.Environment()
The environment is your simulation's universe. It tracks time. It schedules events. It runs the show. Every SimPy simulation needs one.
The Process
def say_hello(env):
yield env.timeout(1)
print(f"Hello at time {env.now}")
A process is a Python generator function (that's what the yield keyword indicates). Processes describe what happens and when. This one waits 1 time unit, then prints a message.
Running the Simulation
env.process(say_hello(env))
env.run()
We register the process with the environment, then run until all processes complete.
A More Interesting Example
Let's simulate a car that drives, parks, and drives again:
import simpy
def car(env):
while True:
print(f"Start driving at {env.now}")
driving_duration = 5
yield env.timeout(driving_duration)
print(f"Start parking at {env.now}")
parking_duration = 10
yield env.timeout(parking_duration)
env = simpy.Environment()
env.process(car(env))
env.run(until=30)
Output:
Start driving at 0
Start parking at 5
Start driving at 15
Start parking at 20
The car drives for 5 units, parks for 10, and repeats. Simple. Predictable. Simulable.
Multiple Processes
Real simulations have many things happening at once:
import simpy
def customer(env, name):
print(f"{name} enters shop at {env.now}")
yield env.timeout(3)
print(f"{name} leaves shop at {env.now}")
env = simpy.Environment()
env.process(customer(env, "Alice"))
env.process(customer(env, "Bob"))
env.process(customer(env, "Charlie"))
env.run()
All three customers enter at time 0 and leave at time 3. They're running in parallel (simulation parallel, not actual threading).
Staggered Arrivals
Let's make it more realistic—customers arrive at different times:
import simpy
def customer(env, name, arrival_time):
yield env.timeout(arrival_time)
print(f"{name} arrives at {env.now}")
yield env.timeout(3)
print(f"{name} leaves at {env.now}")
env = simpy.Environment()
env.process(customer(env, "Alice", 0))
env.process(customer(env, "Bob", 2))
env.process(customer(env, "Charlie", 5))
env.run()
Now they arrive at times 0, 2, and 5. Each spends 3 units shopping before leaving.
Key Concepts Learned
You've now grasped the fundamentals:
- Environment - The simulation clock and scheduler
- Processes - Generator functions that model behaviour
- Timeouts - Events that pause a process for a duration
- yield - The keyword that hands control back to SimPy
Understanding leads to confidence. Confidence leads to experimentation. Experimentation leads to real simulations.
What's Next?
You've written your first simulation. Now build something useful:
- SimPy in 10 Minutes - A broader overview
The hello world is behind you. The real work—and the real fun—starts now.
Ready to Master SimPy?
Get started with simulation in Python using SimPy with my comprehensive guide. It covers everything you need to know to build your first simulation models.
Get the SimPy Guide