educative.io

Using Threads - The Hacker's Guide to Scaling Python

The thread example when I run with python threading-start.py, I always saw same output.
But when I tried with python2 threading-start.py, the order changes.
So, what is the major change in Python3 from threading perspective?


Course: The Hacker's Guide to Scaling Python - Learn Interactively
Lesson: Using Threads - The Hacker's Guide to Scaling Python

Hi @Suresh_K_L, Thanks for reaching out to us.
In python2 if you run the example threading-start.py multiple times, you will notice that the output might be different each time. If you specifically expected any one of the outputs each time, then you forgot that there is no guarantee regarding the order of execution for the threads. Once started, the threads join: the main thread waits for the second thread to complete by calling its join method. Using join is handy in terms of not leaving any threads behind.
If you do not join all your threads and wait for them to finish, it is possible that the main thread finishes and exits before the other threads. If this happens, your program will appear to be blocked and will not respond to even a simple KeyboardInterrupt signal. However, in Python 3, the threading effect is not easily observable using small-scale examples.

Hope this helps.
Happy learning :slight_smile:

Amazed by the response within few hours from the post!! Thank you @Muntha_Amjad


Course: The Hacker's Guide to Scaling Python - Learn Interactively
Lesson: Daemon Processes - The Hacker's Guide to Scaling Python

@Suresh_K_L
You are most welcome. I’m glad I could be a help to you today.

Happy Learning :slight_smile: