The _thread Module The _thread basic module provides us with the same programming interface no matter on which operating system our code runs. When calling the start_new_thread function on this module we indirectly start a new separated thread. The function we pass over to this method will be the main function of this new thread.
The _thread Module import _thread def do_something(thread_id): while True: print('doing something... thread id is ', thread_id) def start_program(): i= 0 while True: i += 1 _thread.start_new_thread(do_something, (i,)) if input() == 'q': break start_program()
The Thread Class The threading module includes the Thread class. We can easily create new thread by defining a new class that extends it, overriding the run function and make sure that our __init__ in the new class calls the __init__ function it overrides.
The Thread Class import threading import time class MyThread(threading.Thread): def __init__(self,str): threading.Thread.__init__(self) self.name = str def run(self): i = 1 while(i