Start working with threads in Python

As mentioned briefly in the previous section, thread-based parallelism is the standard way of writing parallel programs. However, the Python interpreter is not fully thread-safe. In order to support multithreaded Python programs, a global lock called the Global Interpreter Lock (GIL) is used. This means that only one thread can execute the Python code at the same time; Python automatically switches to the next thread after a short period of time or when a thread does something that may take a while. The GIL is not enough to avoid problems in your own programs. Although, if multiple threads attempt to access the same data object, it may end up in an inconsistent state.

In this recipe, we simply show you how to create a single thread inside a Python program.

How to do it…

To execute this first example, we need the program helloPythonWithThreads.py:

## To use threads you need import Thread using the following code:
from threading import Thread

##Also we use the sleep function to make the thread "sleep" 
from time import sleep

## To create a thread in Python you'll want to make your class work as a thread.
## For this, you should subclass your class from the Thread class
class CookBook(Thread):
 def __init__(self):
 Thread.__init__(self)
 self.message = "Hello Parallel Python CookBook!!\n"

##this method prints only the message 
 def print_message(self):
 print (self.message)

##The run method prints ten times the message 
 def run(self):
 print ("Thread Starting\n")
 x=0
 while (x < 10):
 self.print_message()
 sleep(2)
 x += 1
 print ("Thread Ended\n")


#start the main process
print ("Process Started")

# create an instance of the HelloWorld class
hello_Python = CookBook()

# print the message...starting the thread
hello_Python.start()

#end the main process
print ("Process Ended")

To run the example, open the calling_Process.py program with the Python IDE and then press the F5 button on the keyboard.

You will see the following output in the Python shell:

How to do it…

How it works…

While the main program has reached the end, the thread continues printing its message every two seconds. This example demonstrates what threads are—a subtask doing something in a parent process.

A key point to make when using threads is that you must always make sure that you never leave any thread running in the background. This is very bad programming and can cause you all sorts of pain when you work on bigger applications.