Introduction to Threading Module
Get introduced to threading, logging module, custom subclasses and their implementation.
The threading
module was first introduced in Python 1.5.2 as an
enhancement of the low-level thread
module. The threading
module
makes working with threads much easier and allows the program to run
multiple operations at once.
Note that the threads in Python work best with I/O operations, such as
downloading resources from the Internet or reading files and directories
on your computer. If you need to do something that will be CPU
intensive, then you will want to look at Python’s multiprocessing
module instead. The reason for this is that Python has the Global
Interpreter Lock (GIL) that basically makes all threads run inside of
one master thread. Because of this, when you run multiple CPU intensive operations with threads, you may find that it actually runs slower.
So we will be focusing on what threads do best: I/O operations!
Intro to threads
A thread lets you run a piece of long running code as if it were a separate program. It’s kind of like calling subprocess
except that
you are calling a function or class instead of a separate program. We
always find it helpful to look at a concrete example.
Simple example of threading module
Let’s take a look at something that’s really simple:
Get hands-on with 1400+ tech skills courses.