An example in Python

To illustrate the concept of running multiple threads in the same process, let's look at a quick example in Python. If you have already downloaded the code for this book from the GitHub page, go ahead and navigate to the Chapter10 folder. Let's take a look at the Chapter10/my_thread.py file, as follows:

# Chapter10/my_thread.pyimport threadingimport timeclass MyThread(threading.Thread):    def __init__(self, name, delay):        threading.Thread.__init__(self)        self.name = name        self.delay = delay    def run(self):        print('Starting thread %s.' % self.name)        thread_count_down(self.name, self.delay)        print('Finished thread %s.' % self.name)def thread_count_down(name, delay):    counter = 5    while counter:        time.sleep(delay) print('Thread %s counting ...

Get Advanced Python Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.