Common SimPy Errors and How to Fix Them

Every SimPy error message has a story. Here are the ones you'll meet.

Error 1: Forgot to Yield

The most common mistake:

# WRONG
def customer(env, server):
    env.timeout(5)  # Missing yield!
    print("Done")

# RIGHT
def customer(env, server):
    yield env.timeout(5)
    print("Done")

Symptom: Process completes instantly. Time doesn't advance.

Fix: Add yield before every SimPy event.

Error 2: Generator Not Started

# WRONG
env.process(customer)  # Missing parentheses!

# RIGHT
env.process(customer(env, server))

Error message: TypeError: cannot create 'generator' instances

Fix: Call the function to create the generator.

Error 3: Yielding Non-Event

# WRONG
def process(env):
    yield 5  # Number, not event!

# RIGHT
def process(env):
    yield env.timeout(5)

Error message: ValueError: ...is not an event

Fix: Yield SimPy events, not raw values.

Error 4: Using Return Instead of Yield

# WRONG
def customer(env, server):
    with server.request() as req:
        return req  # Should be yield!
        yield env.timeout(5)

# RIGHT
def customer(env, server):
    with server.request() as req:
        yield req
        yield env.timeout(5)

Symptom: Process exits immediately without waiting.

Fix: Use yield to wait, return to exit.

Error 5: Resource Request Outside With

# WRONG
def customer(env, server):
    req = server.request()
    yield req
    yield env.timeout(5)
    # Resource never released!

# RIGHT
def customer(env, server):
    with server.request() as req:
        yield req
        yield env.timeout(5)
    # Auto-released here

Symptom: Resources stay busy forever. Queue grows infinitely.

Fix: Use with statement or explicitly release.

Error 6: Forgot to Start Process

# WRONG
def arrivals(env, server):
    while True:
        yield env.timeout(5)
        customer(env, server)  # Never started!

# RIGHT
def arrivals(env, server):
    while True:
        yield env.timeout(5)
        env.process(customer(env, server))

Symptom: Customers created but never execute.

Fix: Wrap new processes with env.process().

Error 7: Environment Mismatch

# WRONG
env1 = simpy.Environment()
env2 = simpy.Environment()
server = simpy.Resource(env1, capacity=2)

def customer(env, server):
    yield server.request()  # server is from env1!

env2.process(customer(env2, server))
env2.run()  # Boom!

Error message: Events from different environments

Fix: Use one environment per simulation.

Error 8: Modifying Resource During Request

# WRONG
def customer(env, server):
    with server.request() as req:
        yield req
        server.capacity = 5  # Don't do this!
        yield env.timeout(5)

Symptom: Unpredictable behaviour, incorrect statistics.

Fix: Don't modify resource properties during simulation.

Error 9: Infinite Loop Without Yield

# WRONG
def arrivals(env):
    while True:
        print("Arrival")  # No yield - infinite loop!

# RIGHT
def arrivals(env):
    while True:
        yield env.timeout(5)
        print("Arrival")

Symptom: Python hangs. No output.

Fix: Every loop iteration must yield an event.

Error 10: Container/Store Amount Errors

# WRONG - get more than available
container = simpy.Container(env, capacity=100, init=50)

def greedy(env, container):
    yield container.get(200)  # Waits forever!

# RIGHT - check availability
def careful(env, container):
    if container.level >= 50:
        yield container.get(50)

Symptom: Process waits indefinitely.

Fix: Check levels before large requests, or use timeout.

Error 11: Wrong Event Syntax

# WRONG
yield env.timeout == 5
yield env.timeout = 5

# RIGHT
yield env.timeout(5)

Error message: Various syntax or type errors

Fix: timeout is a method - call it with parentheses.

Error 12: Priority Inversion

# WRONG - expecting high priority to always win
def process(env, resource, priority):
    with resource.request(priority=priority) as req:
        yield req
        # Lower priority process might be here first!

Symptom: Low priority processes served before high priority.

Reason: Priority only matters in the queue. First-come wins if resource is free.

Fix: Use PreemptiveResource if you need immediate priority.

Error 13: Cancelled Request Not Handled

# WRONG
def impatient_customer(env, server):
    req = server.request()
    result = yield req | env.timeout(5)

    # If we timed out, req is still pending!
    yield env.timeout(10)
    # Resource might be granted later, causing issues

# RIGHT
def impatient_customer(env, server):
    req = server.request()
    result = yield req | env.timeout(5)

    if req not in result:
        req.cancel()  # Clean up!
    else:
        yield env.timeout(10)
        server.release(req)

Fix: Always cancel abandoned requests.

Error 14: Seed Not Set

# WRONG - different results each run
def arrivals(env):
    while True:
        yield env.timeout(random.expovariate(1))

# RIGHT - reproducible
random.seed(42)
def arrivals(env):
    while True:
        yield env.timeout(random.expovariate(1))

Symptom: Can't reproduce results. Debugging impossible.

Fix: Set random seed before simulation.

Error 15: Run Until vs Run Duration

# WRONG - runs forever if no events
env.run()

# RIGHT - specify duration
env.run(until=1000)

# OR - run until event
done = env.event()
env.run(until=done)

Symptom: Simulation runs forever or stops immediately.

Fix: Always specify when to stop.

Quick Diagnostic Checklist

When something's wrong:

  1. ✓ Did I yield every event?
  2. ✓ Did I call the process function?
  3. ✓ Did I wrap new processes with env.process()?
  4. ✓ Am I using one environment consistently?
  5. ✓ Are resources released properly?
  6. ✓ Is my random seed set?
  7. ✓ Did I specify when to stop?

Summary

Most SimPy errors fall into patterns: - Missing yield - Missing env.process() - Resource management - Environment consistency

Learn these patterns. Fix them once. Move on.

Next Steps


Strengthen Your Python Skills

If you're finding Python tricky, get up to speed quickly with the 10-Day Python Bootcamp. It's designed to give you the confidence and skills to write clean, effective code.

Start the Python Bootcamp