MENU BAR

Saturday, 4 October 2014

Python Threads made easy

Up and running with python threads:
I have taken the pain to write this blog post, as i clearly understand how intimidating it is for a beginner to work with threads in python. I have had an experience no different, when i started picking up python.

In python you can work with threads by using these two modules :
1. thread (deprecated )
2.threading

Implementations :

i am definitely not going to bore you with too much theory . Lets quickly jump into an example .

a. Creating threads using the thread module

Problem Statement :
Create 2 threads named FIRST THREAD
& SECOND THREAD using the thread module in python.Both threads use a
function func() which accepts thread name and delay time as parameter
.
Now iterate each thread 5 times and within the loop print the following information
1. iteration number
2. thread name
3. current system time

Each iteration of the thread should be delayed for a particular time interval.
Consider delay =3 for FIRST THREAD and delay =5 for SECOND THREAD.

Please do read the above code thoroughly before proceedingOUTPUT :

** output on your system might be different from the output that i obtained.

did you notice any peculiarity in the output pattern ? ?

if you did not , then match the output with the code again .

What you will observe is that the line

print("******** All the tasks have finished executing *****") has been printed first ,when it was supposed to be printed after the threads finished executing, right ?

This does not happen as the functions of starting both the threads are happening concurrently . The proof is the overlapping output sequence .

It is not that first the FIRST THREAD will finish and then the next will start, but both of them are happening concurrently.Here the main program is also another thread , so the line print("******** All the tasks have finished executing *****")happens and gets printed without waiting for the other threads to complete. All the threads work asynchronously on their own .

b. Creating threads using the threading module

Now having worked with the thread module , you see that there are a lot of limitations . You cannot wait till all the threads have finished executing and then print some completion message . The threading module is comparatively new and has a lot more features .

Now let us consider the last problem only for simplicity sake and implement it using the threading module .

The slight difference you notice in this code is that we have used the functions start() and join(). Now the start() is similar to start_new_thread() while the join() is a kind of a wait funtion. The t1.join() stops the main program i.e the main thread to continue its execution of t1 is complete , same is the case with t2.

Now lets observe the output

now do you notice print("******** All the tasks have finished executing *****")
gets printed only after all the jobs are complete ? . This was accomplished using join() .

for further readings do look up the official documentation at python.org .

**Now There is a bad boy in this program that affects the system performance drastically while the program is executing :

Here is the video .

if you have any doubts regarding this post , or if you want me to create posts on certain topics , then please do leave it in the comments section .