Event
gradysim.simulator.event
As an event-based simulator one of the main components in the simulation is an event loop, that's the focus of this module. Events are compact classes containing a timestamp and a callback. Events are inserted into the event loop which is organized as a heap to keep the events with the smallest timestamps on top. At every simulation iteration the simulator class grabs the event with the smallest timestamp and executes its callback.
Events are created by handlers. Protocols indirectly interact with them through the provider interface they have access to. These events, when executed, cause effects on the network nodes, mainly observed through calls to the protocol interface methods like handle_timer.
Event
dataclass
Dataclass representing a single event. Will be placed inside the simulation loop. Shouldn't be instantiated
directly, but through the EventLoop.schedule_event
method.
Source code in gradysim/simulator/event.py
callback: Callable
instance-attribute
Any callable with no parameters. Will be executed when the event fires
context: str
instance-attribute
Context in which the event will be executed. This is used in logging to identify where the callback is being executed.
timestamp: float
instance-attribute
Simulation time in seconds when the event will fire
EventLoop
Event loop central to the event-based simulation. Is implemented as a min-heap populated by Event
instances ordered
by their timestamps. Generally only the Simulator
will call the pop_event
method, a handler should only need to use the schedule_event
method.
Source code in gradysim/simulator/event.py
current_time: float
property
Returns the timestamp of the last event to be removed from the heap. This represents the current simulation time.
Returns:
Type | Description |
---|---|
float
|
The current simulation fime |
__init__()
__len__()
By calling len() on the EventLoop you can check how many events are queued in the event heap.
Returns:
Type | Description |
---|---|
int
|
Number of events in the event heap |
clear()
peek_event()
Peeks at the event at the top of the event heap without removing it. Returns None if the event heap is empty.
Returns:
Type | Description |
---|---|
Optional[Event]
|
The event at the top of the event heap or None if it's empty |
Source code in gradysim/simulator/event.py
pop_event()
Removes an event from the top of the event heap and returns it. If called when the event heap is empty it will raise EventLoopException.
Returns: The event popped.
Source code in gradysim/simulator/event.py
schedule_event(timestamp, callback, context='')
Creates an event instance with the information provided as args and inserts it into the event heap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamp |
float
|
Simulation time in seconds when the event should fire |
required |
callback |
Callable
|
Any callable with no arguments |
required |
context |
str
|
Context where the callable executes. Useful for logging. |
''
|