Multithreading allows an application to have multiple flows of control that are executed simultaneously. On the Raspberry Pi, this can be useful in applications that need to monitor the General Purpose Input and Output (GPIO) pins to react to the changes in switch and sensor states.
Multithreading is a large subject that can take a long time to cover completely, hence only a couple of the Python classes in the threading
module will be covered here. However, the full documentation for this module is available at https://docs.python.org/2/library/threading.html.
First, we will import the required modules for the functionality that we will be using and will set up the default logger so that we can see the process ID when a log is written to it. You don't need to worry about this now, as we will be going over the logging framework in Python in a later chapter.
import logging import threading import time logging.basicConfig(level=logging.INFO, format="%(asctime)s...