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.
pip install simpyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a simple 'car' process that alternately parks and drives. It showcases environment creation, defining a process as a generator function, scheduling the process, and running the simulation for a specified duration. The `env.timeout()` event is used to simulate the passage of time.
Ensure Python >= 3.8. Replace `simpy.BaseEnvironment` with `simpy.Environment`. Update imports and code accordingly.
Replace `env.exit(value)` or `raise simpy.exceptions.StopProcess(value)` with `return value` within generator functions.
Rewrite process functions to be generator functions that yield event objects from the environment (e.g., `env.timeout()`, `resource.request()`). Consult the SimPy 2 to 3 porting guide if migrating older code.
Ensure your problem fits a discrete-event, process-based model with interacting components. For fixed-step or continuous simulations without complex interactions, other tools might be more suitable.
Familiarize yourself with Python generators and coroutines. Remember that code within a process generator only executes up to a `yield` statement, then resumes when the yielded event is processed.
Call the process function with its arguments when passing it to `env.process()` to create a generator object.
```python
import simpy
def my_process(env):
yield env.timeout(1)
env = simpy.Environment()
env.process(my_process(env)) # Correct: call the function
env.run()
```Use `simpy.Resource` for managing discrete resource units (like servers or machines) with `request()` and `release()` methods. For `simpy.Container`, use `get()` and `put()` to manage quantities.
```python
import simpy
env = simpy.Environment()
resource = simpy.Resource(env, capacity=1) # Use simpy.Resource for request/release
def process(env, res):
with res.request() as req:
yield req
print(f'{env.now}: Resource obtained!')
env.process(process(env, resource))
env.run()
```Rewrite the process function using a standard `def` and `yield` for SimPy events, instead of `async def` and `await`.
```python
import simpy
def my_process(env): # Standard generator function
print(f'{env.now}: Process started')
yield env.timeout(1) # Use yield for SimPy events
print(f'{env.now}: Process finished')
env = simpy.Environment()
env.process(my_process(env))
env.run()
```No dependency data recorded yet.