simpy.events — Core event types¶
This module contains the basic event types used in SimPy.
The base class for all events is Event. Though it can be directly
used, there are several specialized subclasses of it.
Event(env) |
An event that may happen at some point in time. |
Timeout(env, delay[, value]) |
A Event that gets triggered after a delay has passed. |
Process(env, generator) |
Process an event yielding generator. |
AnyOf(env, events) |
A Condition event that is triggered if any of a list of events has been successfully triggered. |
AllOf(env, events) |
A Condition event that is triggered if all of a list of events have been successfully triggered. |
-
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)¶ An event that may happen at some point in time.
An event
- may happen (
triggeredisFalse), - is going to happen (
triggeredisTrue) or - has happened (
processedisTrue).
Every event is bound to an environment env and is initially not triggered. Events are scheduled for processing by the environment after they are triggered by either
succeed(),fail()ortrigger(). These methods also set the ok flag and the value of the event.An event has a list of
callbacks. A callback can be any callable. Once an event gets processed, all callbacks will be invoked with the event as the single argument. Callbacks can check if the event was successful by examining ok and do further processing with the value it has produced.Failed events are never silently ignored and will raise an exception upon being processed. If a callback handles an exception, it must set
defusedtoTrueto prevent this.This class also implements
__and__()(&) and__or__()(|). If you concatenate two events using one of these operators, aConditionevent is generated that lets you wait for both or one of them.-
env= None¶ The
Environmentthe event lives in.
-
callbacks= None¶ List of functions that are called when the event is processed.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
- may happen (
-
class
simpy.events.Timeout(env, delay, value=None)¶ A
Eventthat gets triggered after a delay has passed.This event is automatically triggered when it is created.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
-
class
simpy.events.Initialize(env, process)¶ Initializes a process. Only used internally by
Process.This event is automatically triggered when it is created.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
-
class
simpy.events.Interruption(process, cause)¶ Immediately schedules an
Interruptexception with the given cause to be thrown into process.This event is automatically triggered when it is created.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
-
class
simpy.events.Process(env, generator)¶ Process an event yielding generator.
A generator (also known as a coroutine) can suspend its execution by yielding an event.
Processwill take care of resuming the generator with the value of that event once it has happened. The exception of failed events is thrown into the generator.Processitself is an event, too. It is triggered, once the generator returns or raises an exception. The value of the process is the return value of the generator or the exception, respectively.Note
Python version prior to 3.3 do not support return statements in generators. You can use :meth:~simpy.core.Environment.exit() as a workaround.
Processes can be interrupted during their execution by
interrupt().-
target¶ The event that the process is currently waiting for.
Returns
Noneif the process is dead or it is currently being interrupted.
-
is_alive¶ Trueuntil 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
RuntimeErrorin these cases.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
-
class
simpy.events.Condition(env, evaluate, events)¶ An event that gets triggered once the condition function evaluate returns
Trueon the given list of events.The value of the condition event is an instance of
ConditionValuewhich allows convenient access to the input events and their values. TheConditionValuewill only contain entries for those events that occurred before the condition is processed.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:
evaluate(events, processed_count). If it returnsTrue, the condition is triggered. TheCondition.all_events()andCondition.any_events()functions are used to implement and (&) and or (|) for events.Condition events can be nested.
-
static
all_events(events, count)¶ An evaluation function that returns
Trueif all events have been triggered.
-
static
any_events(events, count)¶ An evaluation function that returns
Trueif at least one of events has been triggered.
-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
static
-
class
simpy.events.AllOf(env, events)¶ A
Conditionevent that is triggered if all of a list of events have been successfully triggered. Fails immediately if any of events failed.-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-
-
class
simpy.events.AnyOf(env, events)¶ A
Conditionevent that is triggered if any of a list of events has been successfully triggered. Fails immediately if any of events failed.-
defused¶ Becomes
Truewhen the failed event’s exception is “defused”.When an event fails (i.e. with
fail()), the failed event’s value is an exception that will be re-raised when theEnvironmentprocesses the event (i.e. instep()).It is also possible for the failed event’s exception to be defused by setting
defusedtoTruefrom an event callback. Doing so prevents the event’s exception from being re-raised when the event is processed by theEnvironment.
-
fail(exception)¶ Set exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance.
Raises
ValueErrorif exception is not anException.Raises
RuntimeErrorif this event has already been triggered.
-
ok¶ Becomes
Truewhen the event has been triggered successfully.A “successful” event is one triggered with
succeed().Raises: AttributeError – if accessed before the event is triggered.
-
processed¶ Becomes
Trueif the event has been processed (e.g., its callbacks have been invoked).
-
succeed(value=None)¶ Set the event’s value, mark it as successful and schedule it for processing by the environment. Returns the event instance.
Raises
RuntimeErrorif this event has already been triggerd.
-
trigger(event)¶ Trigger the event with the state and value of the provided event. Return self (this event instance).
This method can be used directly as a callback function to trigger chain reactions.
-
triggered¶ Becomes
Trueif the event has been triggered and its callbacks are about to be invoked.
-
value¶ The value of the event if it is available.
The value is available when the event has been triggered.
Raises
AttributeErrorif the value is not yet available.
-