Skip to content

Process Example

This example illustrates how to use the Component class and the sleep method of the Simulator in a simulation scenario.

Imports

To begin, import the necessary classes from pydes.

from pydes import Component, Simulator

Define Model

Next, define your processes by creating a class that extends the Component class and implements its main method.

Inside the main method, you can control the simulation time and process flow using various methods provided by the Simulator. Here, we use the sleep method to pause execution for a specified duration before continuing with the component's process. While this component is inactive, other processes can progress within the simulation.

Additionally, you may want to record information about the process using the record method of the Simulator.

In this simple example, we're modeling a process that repeatedly sleeps for a given time and then restarts. There are two approaches to model this:

Use an infinite while loop within the main method to encapsulate the process logic.

class Process1(Component):
    def __init__(self, sim: Simulator, time: float = 10):
        self.sim = sim
        self.time = time

    def main(self):
        i = 0
        while True:
            self.sim.record(self, "running loop", 0)
            i += 1
            self.sim.sleep(self.time)

Alternatively, activate the Component within the main method by passing self to the schedule method.

class Process2(Component):
    def __init__(self, sim: Simulator, time: float = 10):
        self.sim = sim
        self.time = time

    def main(self):
        i = 0
        self.sim.record(self, "running loop", 0)
        i += 1
        self.sim.sleep(self.time)
        self.sim.schedule(self)

Run Simulation

To execute the simulation with these two different processes, create a Simulator object and schedule both components.

print("Process 1")
sim = Simulator()
process1 = Process1(sim, time=10)
sim.schedule(process1)
sim.run(30)
print("Process 2")
sim.reset()
process2 = Process2(sim, time=10)
sim.schedule(process2)
sim.run(30)

The simulation output would resemble the following:

Process 1
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time                           | component       | value                                    | description                    |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
|                                |                 |                                          |                                |
| 0                              | Process1.0      | running loop                             | None                           |
| 10                             | Process1.0      | running loop                             | None                           |
| 20                             | Process1.0      | running loop                             | None                           |
| 30                             | Process1.0      | running loop                             | None                           |
Process 2
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
| time                           | component       | value                                    | description                    |
| ------------------------------ | --------------- | ---------------------------------------- | ------------------------------ |
|                                |                 |                                          |                                |
| 0                              | Process2.0      | running loop                             | None                           |
| 10                             | Process2.0      | running loop                             | None                           |
| 20                             | Process2.0      | running loop                             | None                           |
| 30                             | Process2.0      | running loop                             | None                           |