What is SimPy? How to run python simulations?

Over the years, the world has seen several advancements in technology. Various systems developed to enhance efficiency and reduce human effort. When it comes to air transport and road transport, systems need high productivity to prevent delays and congestion. The systems need optimization to enhance their efficiency and to overcome any glitch that might arise. This is where Python Simulations come into the picture. The optimization occurs through models supported by the SimPy framework.

Airports and healthcare management involve large amounts of data. There is a need to monitor every individual. In such circumstances, Python simulation models have an immense role to play. Before we proceed further on how this gets done, let’s get an insight into what SimPy is and how it works.

What is SimPy?

Most enterprises use SimPy as it is an open-source, object-oriented, and simulation library dealing with resource management 24x7. From managing patient history in the healthcare industry to keeping track of assets in a financial enterprise, SimPy does it all. Though SimPy uses Python programming completely, you can read it using Java Virtual Machine. It can also get decoded using .Net. Before running a SimPy program, there are certain prerequisites that you should keep in mind.

Things to know before using SimPy

There are certain prerequisites that you should consider before using SimPy. The first and foremost thing is you should have a better understanding of Python language. You should be well versed in all the fundamentals, classes, and generators. Generators get attached to the main function and also its variables. With the help of the generator, the function can execute yield statements and generate results for the given expression.

The yield statements are nothing but tools used for scheduling or processing a specific set of information. For instance, if a batch of vehicles needs manufacturing, the yield statements set up a schedule for manufacturing each vehicle in the batch. Only when the batch is sent, the next batch gets manufactured. Based on the type of operations, these statements are named as:

Yield Request: The system adds the command to the waitlist. Once the operation gets done, the next command executes.

Yield Passivate: This command makes the system wait until any other command executes it.

Yield Hold: This command gets used to executing a specific set of operations over a definite period.

Yield Release: This command constantly updates the processes once completed, thereby making sure that there is no delay in carrying out the next set of operations.

A basic understanding of Python is mandatory. The next thing is you have got to install the right package. SimPy is the main framework that you’ll be using for the system. This package will be pivotal in creating, managing, and running the simulation. SimPy is nothing but a product of data science. You might probably know that data science involves statistics implemented with programming. You’ll be using statistics to calculate the average wait time and to come up with random numbers. The statistics and the random modules are a part of the Python library, thereby reducing the workload of downloading any new module.

Before simulating, you also get to choose in which way you’ll be running the program. You have to choose either to run it interactively or run it in a shell. When running it interactively, you’ll be using a Jupyter Notebook, where each block comprises its class definition. Whereas while running it in a shell, you have to save the simulation in .py format and the Python has to simulate the terminal. The result shall be the same irrespective of whichever method you choose.

Wait, there is still more. Before writing the code, you get to know what kind of a process you’ll be running and what will be the final result. This will give you a clear idea of what the end customer might experience. You have to develop a step-by-step iteration for any process. For instance, if you are going to a theatre, you gotta start by buying a ticket to getting a seat at the theatre. This might have given you a clear idea of how SimPy works.

How to run Python Simulations?

After coding the program, you can execute it with the help of simple functions and classes. Your simulation will come to life once your code is free of errors. For instance, if you have to write a code to determine the standard waiting time of a customer in a theatre, all you need do is use specific functions and classes in your program. You might have to use the following functions and classes in your program.

Theatre: You can use this as a class definition to denote what kind of operations are to be carried out and provide information about the class such as what type of environment it is and what kind of resources are available in that specific environment.

go_to_movie(): This function makes a direct request for employing a resource. It has to go through the entire process. Once done it liberates it to the next movie getter.

run_theater(): The function is responsible for running the program. It creates a replica of the theatre class and uses go_to_movie() function to generate the result and thereby the function makes people go through the entire process that is from collecting the ticket counter to grabbing a seat in the theatre.

get_average_wait_time(): The average time it takes for a moviegoer to make it through the theatre is determined using the get_average_wait_time() function.

calculate_wait_time(): This function makes sure that the user can read and analyze the final result.

get_user_input(): You can provide information and assign certain parameters using the get_user-input() function. The information might be on the number of ticket collectors or cashiers that are available in the theatre. It can also be on the number of available seats.

main(): Proper functioning of programs in the command line is regulated using this function

Now all you’ve got to do is write two more codes to bring your program to life.

if_name_= ‘ _main_’:


Navigate to where you stored simulate.py and run the program. This is the basic framework to run the program. You can employ this with any sort of system and make it quite efficient and productive.

Closing Thoughts

As mentioned earlier, SimPy is a product of data Science. Data Science has emerged as the next big thing, and many individuals are opting for a career in data science. From top-notch MNCs to mediocre startups, almost every industry has data science jobs and is looking out for quality data scientists. Over the past decade or so, there has been a vast emergence of data science companies across the world and the future of data science is bright. With the ongoing COVID-19 pandemic, the only job that is at bloom is of a data science professional. So if you are looking for a data science career out of it, you can’t find a better opportunity than this.

All you need do is equip yourself with the necessary skills. With the global pandemic in place, you can opt for free data analysis courses. There are so many job vacancies that are to be filled across the globe. Make sure that you make the best use of this pandemic and engulf in equipping yourself with the necessary skills. To learn data science, you can opt for a premier educational institution like Great Learning. Being excellent learning sources, our data science courses can give you a holistic learning experience.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My view on Roadmaps in 2020

Call with Current Continuation as Recursive Return

Industrial (factory-like) production is base on two key concepts "replaceable parts" and "assembly…

The responsibilities of an open-source developer

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Explore the possibilities with hundreds of domain extensions — {link} -

SpartanDev: 16/08/21–22/08/21

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Divyas S

Divyas S

More from Medium

Start your career with these Data Science Skills.

Data science in Eve Online

My Top Five Data Science Applications in Finance; Add yours in the comments!

Short checklist for a small data science project