Every process running inside an operating system contains at least one thread, and can further initiate multiple threads, which are then executed simultaneously in the same process space. This is similar to executing multiple applications at the same time. In Python threading can be used to run several function calls or other tasks concurrently, especially if those tasks involve some waiting times. This is the case for networking applications and web related tasks. Among others. Threading is a large and advanced topic in Python, and that's why we will discuss only the basics of threading in this course.
Python provides a specific module for threading purposes. This module is simply called threading and it is a built in module. Of course, before using this module, you should first imported using the same import statement that we have discussed so far from within that module we will use the thread class return with a capital T prepended. Of course by the module name from within this class, the most useful and used methods are start and join, the start method simply starts or initiates a thread. The join method makes sure the program waits for all threads to terminate. Okay, let's see a simple example of threading in Python.
And let's create a file in which to write our code. I will name this file my threads.py and save it to the D drive of my computer. First, we should import the threading module as always, so import threading. Also, we will need the module we haven't talked about yet the time module. So we will import this module as well. From within the time module we will need the sleep method which basically interrupts the execution of the program for as many seconds as you enter in between its parentheses.
So for instance, time dot sleep of one will hold the execution for us. And I said that threading can be used to run multiple function calls at the same time. So let's define a function that prints something, then wait for three seconds, and then print something else. I will use this simple function just to prove that using threading, you can run multiple function calls simultaneously. Okay, this is the function now it's time to configure threading. But first, let's set a goal for this application, we would like to run my function five times concurrently.
And as a bonus, we would like the program to wait for all threads to terminate. To achieve our goal, I will first create an empty list called let's say threads. Then because we want five concurrent instances of the function to be executed, we can make use of a for loop and the range function for i in range of five. Now let's use the thread class from the threading module and tell it the target function to be executed. For this we will use an argument called Target. So let's say T h equals threading the name of the module dot thread, the name of the class, and in between its parentheses, let's pass this parameter right here, target equals, and now the name of our function, the function to be executed my function.
Okay, next, it's time to start the thread and then also append each threading object to the threads list. As I said at the beginning of this lecture, you will use the start method to initialize a thread. So the age dot start, and now let's append threads dot append d h. Finally, using another for loop to iterate over the threats list, we will use the join method in order to instruct the program to wait for all threads to finish for th in threads in the list we defined simply do a th dot join. Okay, now let's save the file and then run it in the windows command. line to see threading inaction. Okay, so Python D, my threads dot p y.
And now let's watch the execution of this program carefully. Okay, so this clearly demonstrates that the function is called Five times concurrently, because it printed out the first string five times, then it stopped for three seconds, and then printed out the second string five times, if we would have called the function of five times without using threading, then what would have happened? Well, the first call would have printed the first string, then waited three seconds, then printed the second string, then all the subsequent function calls would have done the same thing. So a total of 15 seconds of interruption. So let's see this by simply deleting any threading related code in our program, and then just execute it again. So let's delete this.
And let's say for i in range of five, just run my phone Okay, let's save the file. And now let's run it again. So there it is the difference between normal function calling and threaded function calling, which can be very useful in some scenarios and can save you a lot of time when running your applications. I think you now have a pretty good idea about threading. So let's move on to the next lecture.