Thread in Python – Join (part 2)

In this second part of the Thread in Python series, we will see how it is possible to influence the execution of multiple threads thanks to the use of Join.

 

Join()

Let’s modify the previous example to have more control over each individual thread. We create two threads that will wait for two different times, for example 10 and 4 seconds. In this way I will always know that the first thread will last longer than the second.

Furthermore both threads will last longer than the execution of the program. The two seconds added at the end of the program I need to make sure that the program ends after the creation of the second thread.

Executing the code …

Thread in Python - Join

It is very clear that the program starts the two threads and then ends after two seconds. Instead the two threads will continue to work. First the second thread will end and then the first one. Everything behaves as we wanted. By performing several times, we will always have the same result.

But what if we wanted a different sequence? For example we want the program to wait for the end of the second thread before it stops.

The thread.join() function exists in the threading module. This launched in the thread will force the program to wait for the execution of the thread to complete before it can be closed.

We then add the join to the second thread to the code first.

And execute it.

Thread in Python - Join 2

It is clear that the program will now wait for the end of the second thread before finishing.

If instead we use the join () for the first thread.

And executing ..

Thread in Python - Join 3

We see that the program will now wait for the end of the first thread, which being the one that takes the most time, we will have all the threads terminated before the closure of the program.

Now, except for this particular case, in which we know very well the duration of each thread (we have forced things …), it is almost impossible to predict the times and durations of each thread. If we want the program to wait for all threads to complete, it’s a good idea to add joins to all the individual threads.

Conclusions

In this second part we have seen the role of the Join within the threads and how these can influence the execution of multiple threads together. In the next part, we will further explore the topic of threads, introducing the Multithreading itself.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.