simpy.events — Core event types

This events module contains the various event type used by the SimPy core.

The base class for all events is Event. Though it can be directly used, there are several specialized subclasses of it:

  • Timeout: is scheduled with a certain delay and lets processes hold their state for a certain amount of time.
  • Initialize: Initializes a new Process.
  • Process: Processes are also modeled as an event so other processes can wait until another one finishes.
  • Condition: Events can be concatenated with | an & to either wait until one or both of the events are triggered.
  • AllOf: Special case of Condition; wait until a list of events has been triggered.
  • AnyOf: Special case of Condition; wait until one of a list of events has been triggered.

This module also defines the Interrupt exception.

simpy.events.PENDING = object()

Unique object to identify pending values of events.

simpy.events.URGENT = 0

Priority of interrupts and process initialization events.

simpy.events.NORMAL = 1

Default priority used by events.

class simpy.events.Event(env)

Base class for all events.

Every event is bound to an environment env (see BaseEnvironment) and has an optional value.

An event has a list of callbacks. A callback can be any callable that accepts a single argument which is the event instances the callback belongs to. This list is not exclusively for SimPy internals—you can also append custom callbacks. All callbacks are executed in the order that they were added when the event is processed.

This class also implements __and__() (&) and __or__() (|). If you concatenate two events using one of these operators, a Condition event is generated that lets you wait for both or one of them.

env = None

The Environment the event lives in.

callbacks = None

List of functions that are called when the event is processed.

triggered

Becomes True if the event has been triggered and its callbacks are about to be invoked.

processed

Becomes True if the event has been processed (e.g., its callbacks have been invoked).

value

The value of the event if it is available.

The value is available when the event has been triggered.

Raise a AttributeError if the value is not yet available.

trigger(event)

Triggers the event with the state and value of the provided event.

This method can be used directly as a callback function.

succeed(value=None)

Schedule the event and mark it as successful. Return the event instance.

You can optionally pass an arbitrary value that will be sent into processes waiting for that event.

Raise a RuntimeError if this event has already been scheduled.

fail(exception)

Schedule the event and mark it as failed. Return the event instance.

The exception will be thrown into processes waiting for that event.

Raise a ValueError if exception is not an Exception.

Raise a RuntimeError if this event has already been scheduled.

class simpy.events.Timeout(env, delay, value=None)

An Event that is scheduled with a certain delay after its creation.

This event can be used by processes to wait (or hold their state) for delay time steps. It is immediately scheduled at env.now + delay and has thus (in contrast to Event) no success() or fail() method.

class simpy.events.Initialize(env, process)

Initializes a process. Only used internally by Process.

class simpy.events.Process(env, generator)

A Process is a wrapper for the process generator (that is returned by a process function) during its execution.

It also contains internal and external status information and is used for process interaction, e.g., for interrupts.

Process inherits Event. You can thus wait for the termination of a process by simply yielding it from your process function.

An instance of this class is returned by simpy.core.Environment.process().

target

The event that the process is currently waiting for.

May be None if the process was just started or interrupted and did not yet yield a new event.

is_alive

True until the process generator exits.

interrupt(cause=None)

Interupt this process optionally providing a cause.

A process cannot be interrupted if it already terminated. A process can also not interrupt itself. Raise a RuntimeError in these cases.

class simpy.events.Condition(env, evaluate, events)

A Condition Event groups several events and is triggered if a given condition (implemented by the evaluate function) becomes true.

The value of the condition is a dictionary that maps the input events to their respective values. It only contains entries for those events that occurred until the condition was met.

If one of the events fails, the condition also fails and forwards the exception of the failing event.

The evaluate function receives the list of target events and the number of processed events in this list. If it returns True, the condition is scheduled. The Condition.all_events() and Condition.any_events() functions are used to implement and (&) and or (|) for events.

Conditions events can be nested.

static all_events(events, count)

A condition function that returns True if all events have been triggered.

static any_events(events, count)

A condition function that returns True if at least one of events has been triggered.

class simpy.events.AllOf(env, events)

A Condition event that waits for all events.

class simpy.events.AnyOf(env, events)

A Condition event that waits until the first of events is triggered.

exception simpy.events.Interrupt(cause)

This exceptions is sent into a process if it was interrupted by another process (see Process.interrupt()).

cause may be none if no cause was explicitly passed to Process.interrupt().

An interrupt has a higher priority as a normal event. Thus, if a process has a normal event and an interrupt scheduled at the same time, the interrupt will always be thrown into the process first.

If a process is interrupted multiple times at the same time, all interrupts will be thrown into the process in the same order as they occurred.

cause

The cause of the interrupt or None if no cause was provided.