API reference

The discrevpy module defines a global minimalist discrete event simulator. In simplest terms: it maintains an event queue of callbacks ordered by their scheduled discrete simulation time.

class simulator.Simulator[source]

Discrete event simulator class.

end(delay=0)[source]

Set when the simulator should end even if there are still events in the event heap.

If the delay is zero (or if no delay argument is provided) the current event will be the last event executed if the simulator is RUNNING. Zero delay is not permitted when the simulator is in READY state.

If there are multiple end() calls, the end() call resulting in the earliest end time will be the end time. As such, an end() call cannot be undone.

Parameters

delay (int) – (Optional; default: 0) Delay from current simulation time (now) to end the simulation

Return type

None

event_heap_size()[source]

Retrieve the current size of the event heap.

If the simulator is FINISHED and the run ended due to an end time having been set (via end()), the event heap can still have events in it. In INIT state, the event heap size is always zero.

Return type

int

Returns

Number of events in the event heap

is_finished()[source]

Check whether the simulator has been run and as such is in FINISHED state. When the simulator is FINISHED, it is not possible to schedule events. The simulator can be put into INIT state by calling reset().

Return type

bool

Returns

True iff the simulator is FINISHED

is_init()[source]

Check whether the simulator is in INIT state. When the simulator is INIT, it is not possible to schedule events. The simulator can be put into READY state by calling ready().

Return type

bool

Returns

True iff the simulator is INIT

is_ready()[source]

Check whether the simulator is in READY state. When the simulator is READY, it is possible to schedule events. The simulator can be put into RUNNING state by calling run().

Return type

bool

Returns

True iff the simulator is READY

is_running()[source]

Check whether the simulator is in RUNNING state. When the simulator is RUNNING, it is possible to schedule events. After the run ends the simulator becomes FINISHED.

Return type

bool

Returns

True iff the simulator is RUNNING

now()[source]

Retrieve current simulation time. Before the simulation is run, this will return 0. After the simulation is run, it will return the time of the last executed event if there was no end time, else it will return the end time.

Return type

int

Returns

Current simulation time

ready()[source]

Ready the simulator such that initial events can be scheduled.

Return type

None

reset()[source]

Reset the simulator such that it can be run again. This results in current time (now) being set to 0, the event heap being emptied and any end time being unset.

Return type

None

run()[source]

Run the simulation.

If there is an end time (specified via end()), the simulation will end at that time moment. Else, if there is no end time specified, the simulation will run until there are no more events.

Return type

None

schedule(delay, callback, *args, **kwargs)[source]

Schedule an event in the simulation with default priority (0).

Parameters
  • delay (int) – Delay from current simulation time (now)

  • callback (Union[function, method, builtin_function_or_method]) – Callback: it must be a function or method

  • args – (Optional) Positional arguments passed to the callback

  • kwargs – (Optional) Keyword arguments passed to the callback

Return type

None

schedule_with_priority(delay, priority, callback, *args, **kwargs)[source]

Schedule an event in the simulation with a certain priority.

If there are multiple events in one time moment, the priority determines which goes first. The lower the priority, the earlier it is executed in its time moment. If the priorities of two events are equal, the final arbitration is based on which event was scheduled first.

Parameters
  • delay (int) – Delay from current simulation time (now)

  • priority (int) – Priority

  • callback (Union[function, method, builtin_function_or_method]) – Callback: it must be a function or method

  • args – (Optional) Positional arguments passed to the callback

  • kwargs – (Optional) Keyword arguments passed to the callback

Return type

None