Skip to content

Event Example

This example is about showing the usage of the Event component. This component can be used as a condition that needs to be triggered in order to continue the simulation.

pydes.Event(sim)

An event can be waited and set by components. They are very useful to model trigger conditions and model interaction between different actors across the system.

Parameters:

Name Type Description Default
sim Simulator

The simulator instance.

required

Methods:

Name Description
wait

a component can call the wait method and suspend its excecution until this event is set.

set

a component can call the set method and trigger the event. This causes all the waiting components to continue its excecution.

set()

Set the event.

wait()

Wait for the event to be set.

Imports

First of all, we import the necessary modules and classes from the pydes library. These imports are essential for setting up and running our simulation.

from pydes import Simulator, Component, Event

Define Model

We define Process1, which is one of the example processes in our simulation. Process1 waits for an event to be triggered.

1
2
3
4
5
6
7
8
9
class Process1(Component):
    def __init__(self, sim: Simulator, event: Event):
        self.sim = sim
        self.event = event

    def main(self):
        self.sim.record(self.id, "wait for event")
        self.event.wait()
        self.sim.record(self.id, "event was triggered")

We define Process2, which is the second example process in our simulation. Process2 triggers the event after a specified duration.

class Process2(Component):
    def __init__(self, sim: Simulator, event: Event):
        self.sim = sim
        self.event = event

    def main(self):
        self.sim.record(self.id, "sleeps before triggering event")
        self.sim.sleep(10)
        self.sim.record(self.id, "sets event")
        self.event.set()
        self.sim.record(self.id, "event was set")

Run Simulation

Finally, we set up the simulation environment, schedule the processes, and run the simulation.

sim = Simulator()
event = Event(sim)
p1 = Process1(sim, event)
p2 = Process2(sim, event)
sim.schedule(p1.main)
sim.schedule(p2.main)
sim.run()

The simulation output would resemble the following:

| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time                           | component       | value                                    | description                    |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
|                                |                 |                                          |                                |
| 0                              | Process1.0      | wait for event                           | None                           |
| 0                              | Process2.0      | sleeps before triggering event           | None                           |
| 10                             | Process2.0      | sets event                               | None                           |
| 10                             | Process2.0      | event was set                            | None                           |
| 10                             | Process1.0      | event was triggered                      | None                           |