September 2018
Intermediate to advanced
426 pages
10h 46m
English
Python has an API that allow us to write applications with multiple threads. To get started with multithreading, we are going to create a new thread inside a python class and call it ThreadWorker.py. This class extends from threading.Thread and contains the code to manage one thread:
import threadingclass ThreadWorker(threading.Thread): # Our workers constructor def __init__(self): super(ThreadWorker, self).__init__() def run(self): for i in range(10): print(i)
Now that we have our thread worker class, we can start to work on our main class. Create a new python file, call it main.py, and put the following code in:
import threadingfrom ThreadWorker import ThreadWorkerdef main(): # This initializes ''thread'' as an ...