Get startedGet started for free

SimPy Package: Managing the scheduling of events

1. SimPy Package: Managing the scheduling of events

In this video, we will learn SimPy methods to manage and schedule events.

2. Managing event occurrences

In real-world systems, many situations can affect the occurrence of processes and events. Most of these constraints are related to one of the following situations: conditional events, interruption of events, and waiting for processes. These situations can be represented in a SimPy model. Let's look into each of these cases.

3. Conditional events

Some events depend on others and are only triggered when a condition is met. Let's take the example of a customer arriving at a clothes shop. Customers take time to search for items. This can be clocked-in with the dot-timeout method. However, customers will only buy an item if it is available in stock. This condition can be reflected in the model using an if-statement. Bringing all the code together, we have a generator that simulates the sequence of events that starts with the customer arriving at a shop, then searching for items, and concluding the visit with a conditional purchase.

4. Interruption of events

Interruption of events is also an important situation commonly affecting the outcome of systems. Events can be interrupted due to the occurrence of higher priority events, voluntary interruption (as when someone voluntarily chooses to stop charging an electric car before the battery is full), and forced interruption (as when machines break). The method to interrupt a process is dot-interrupt. Let's take the example of a machine that breaks regularly. Whenever the machine breaks, the processes that depend on it are suspended, and the machine resource won't be available for use until it has been repaired. In this example, we have the generator "break_machine()" where the breaking of the machine is simulated by the function "fail_time()" and clocked in. Once the failure is triggered, the dot-interrupt method is called.

5. Waiting for processes

Now let's look at situations of process dependency. This is when a process can only occur when other processes have been completed. This situation is common when resources are shared between processes. In this example, the resource has a capacity of five, so process requests may have to be queued until one of the five instances of the resource becomes available. Let's take the example of a petrol station. A driver requests a gas pump using the with-statement and yields. However, once such a pump request is granted, the actual refueling process takes time to complete, so other drivers will still have to wait until the pump request is accepted.

6. Concatenate events using bitwise operators

Concatenation of events in SimPy is performed with bitwise operators. Bitwise operators perform bitwise calculations on integers. In other words, they first convert integers into binary before performing operations. They typically expect two operands. In SimPy, this generates a condition event with two events, allowing waiting for both or one of the processes. Relevant for SimPy are bitwise-and and bitwise-or. The first waits for both processes, while the second waits for one of the processes only.

7. Concatenate events using bitwise operators

Let's take the example of a moviegoer. People will wait in the ticket line until it's their turn or until the movie sells out. Like in the previous cases, the ticket request needs to be opened first. In this case, we are using the bitwise-or operator to define the two possible situations, namely my_turn for if tickets are still available and counter-dot-sold_out for otherwise.

8. Let's practice!

Let's put into practice these new SimPy concepts.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.