Threads allow Python programs to handle multiple functions at once as opposed to running a sequence of commands individually. This topic explains the principles behind threading and demonstrates its usage.
Advanced use of multithreads
This section will contain some of the most advanced examples realized using Multithreading.
Advanced printer (logger)
A thread that prints everything is received and modifies the output according to the terminal width. The nice part is that also the "already written" output is modified when the width of the terminal changes.
Basics of multithreading
Using the threading module, a new thread of execution may be started by creating a new threading.Thread and assigning it a function to execute:
The target parameter references the function (or callable object) to be run. The thread will not begin execution until start is called on the Thread object.
Starting a Thread
Now that my_thread has run and terminated, calling start again will produce a RuntimeError. If you'd like to run your thread as a daemon, passing the daemon=True kwarg, or setting my_thread.daemon to True before calling start(), causes your Thread to run silently in the background as a daemon.
Joining a Thread
In cases where you split up one big job into several small ones and want to run them concurrently, but need to wait for all of them to finish before continuing, Thread.join() is the method you're looking for.
For example, let's say you want to download several pages of a website and compile them into a single page. You'd do this:
A closer look at how join() works can be found here.
Create a Custom Thread Class
Using threading.Thread class we can subclass new custom Thread class.
we must override run method in a subclass.
Communicating between threads
There are multiple threads in your code and you need to safely communicate between them.
You can use a Queue from the queue library.
Creating producer and consumer threads with a shared queue
Creating a worker pool
Using threading & queue:
Python Cookbook, 3rd edition, by David Beazley and Brian K. Jones (O’Reilly). Copyright 2013 David Beazley and Brian Jones, 978-1-449-34037-7.