SimPy is a powerful process-based discrete event simulation framework written in Python.
To install SimPy, use the following command –
pip install simpy
Basic Concepts :
The core idea behind SimPy is the generator function in Python. The difference between a normal function and a generator is that a normal function uses the “return” statement, while a generator uses “yield” statement.
If the function has a return statement, then even on multiple function calls, it returns the same value. For eg –
When the func() is called during the runtime, it will always return at the first instance of the return statement, that is, the function func() always returns 1, and the next return statement is never executed.
However, in discrete event simulation, we may need to find the state of the system at a given time T. For that, it is required to remember the state of the interval just before T, and then perform the given simulation and return to state at time T.
This is where generator functions are quite useful. For example, consider the following function
Now, when the first time this function is called, it ‘yields’ 1. However, on the very next call, it will yield 2. In some sense, it remembers what it returned upon the last call, and moves on to the next yield statement.
Events in SimPy are called processes, which are defined by generator functions of their own. These processes take place inside an Environment. (Imagine the environment to be a large box, inside of which the processes are kept.)
Consider a simple example, involving the simulation of a traffic light –
Light turns GRN at 0 Light turns YEL at 25 Light turns RED at 30 Light turns GRN at 90 Light turns YEL at 115 Light turns RED at 120
In this code, the generator function Traffic_Light(env) takes the environment variable as the argument and simulates the operation of the traffic light for the time period passed as argument in the env.run() function. (Actually, time in SimPy is unitless. Though it can be converted to hours, minutes or seconds as per convenience). env.now returns the current value of the time elapsed.
env.timeout() function is the base of this simulation, as it waits for the time passed as the argument to be elapsed on the computer’s simulation clock (it is not a real time clock), and then initiate the next yield statement, till the time passed as argument in env.run() has finished.
env.run() starts all the processes linked to the environment at the same time = 0.
- Basics Of Python Modules
- Draw Circle in Python using Turtle
- Python | Writing to an excel file using openpyxl module
- Part of Speech Tagging with Stop words using NLTK in python
- Get similar words suggestion using Enchant in Python
- Reading an excel file using Python
- Writing to an excel sheet using Python
- Python | Reading an excel file using openpyxl module
- Python | Merge, Join and Concatenate DataFrames using Panda
- Python | Plotting Google Map using folium package
- Python | Data analysis using Pandas
- Testing in Python using doctest module
- Python | Find Live running status and PNR of any train using Railway API
- Python | Plotting Data on Google Map using pygmaps package
- Python | Geographical plotting using plotly
- Python | Plot different graphs using plotly and cufflinks
- Python | ASCII art using pyfiglet module
- Python | Read csv using pandas.read_csv()
- Python - Data visualization using Bokeh
- Python | Arithmetic operations in excel file using openpyxl
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.