SimPy
SimPy is a process-based discrete-event simulation framework based on standard Python. Processes in SimPy are defined by Python generator functions and can, for example, be used to model active components like customers, vehicles or agents. SimPy also provides various types of shared resources to model limited capacity congestion points (like servers, checkout counters and tunnels). It is currently at version 4.1.1 and follows an active release cadence with regular updates.
Warnings
- breaking SimPy 4.0 dropped support for Python 2.7 and requires Python 3.6+ (Python 3.8+ for 4.1.x). `BaseEnvironment` was removed; users should inherit from `Environment` instead.
- breaking In SimPy 4.0, the `Environment.exit()` method and `StopProcess` exception were eliminated. Process generators that need to return a value or exit early must now use the standard Python `return` keyword.
- breaking SimPy 3.x introduced a major API overhaul from SimPy 2.x. Processes no longer needed to subclass `Process` and now yield event objects directly (e.g., `yield env.timeout(1)` instead of `yield hold, self, 1`).
- gotcha SimPy is a discrete-event simulation library. It is not designed for continuous simulations or fixed-step simulations where processes do not interact or use shared resources. Using it for such scenarios can be overkill or lead to unidiomatic code.
- gotcha SimPy processes are Python generator functions. Understanding `yield` is crucial; it suspends a process until an event occurs, returning control to the simulation. Misunderstanding generator execution flow can lead to logical errors.
Install
-
pip install simpy
Imports
- Environment
import simpy env = simpy.Environment()
- Resource
from simpy import Environment, Resource
- Container
from simpy import Environment, Container
- Store
from simpy import Environment, Store
Quickstart
import simpy
def car(env):
while True:
print(f'Start parking at {env.now}')
parking_duration = 5
yield env.timeout(parking_duration)
print(f'Start driving at {env.now}')
trip_duration = 2
yield env.timeout(trip_duration)
env = simpy.Environment()
env.process(car(env))
env.run(until=15)