Different Ways to Kill a Thread in Python

First, we will discuss the thread. So, what is a thread? Threading as the name suggests it’s happening of two or more things at the same time. In Python, threading means one program will have more than one thing at the same time as the program execution. Threads are generally used in multi-threading for the purpose of multi-tasking.

A point to remember about threading is that a Thread can be scheduled during program execution. Thread is also independent of the main program and can be executed individually too.

Threading in Python enables it to execute other programs while being on hold. Below is the Python program on how to use threading in Python by using the threading library.

import threading
import time
print("Values from 10 to 20: ")
def thread():
    for i in range(10, 21):
        time.sleep(1)
        print(i)
threading.Thread(target=thread).start()

Output:

Values from 10 to 20:
10
11
12
13
14
15
16
17
18
19
20

Here in the above Python program first we are importing the thread library using import then we are using the print function to print the text (Value from 1 to 10: ) on the screen. After that, we are creating a function “thread”  using the def keyword.
After creating the function, we are using for loop to read all the values and use time. sleep function.  After that, we are creating a thread by using threading. (“The name of the function created” ) here we have created “thread” as the function.

Ways to kill a Thread:

There are different ways to kill a thread in python. Some of them are:-

1. Use of Exit Flag:

Using an exit flag to kill a thread in python is easier and more efficient than the other methods. we use an exit flag for each thread so that the thread can know when is the time for them to exit the main program.

import threading 
import time 
  
def thread(stop):
    while True: 
        print("RUNNING") 
        if exitflag1: 
            break
exitflag1 = False
t1 = threading.Thread(target = thread, args =(lambda : exitflag1, )) 
t1.start() 
time.sleep(0.1) 
print('Stop the threads.')
exitflag1 =True
t1.join() 
print('TERMINATED!')

Output:

RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
RUNNING
Stop the threads.
RUNNING
TERMINATED!

In the above Python program, we created a function called thread which prints “RUNNING” until the while loop condition is true and till it encounters the “exitflag1=True”. After encountering the exit flag thread can be killed by using the t1.join() method.

You can also refer to How to create a thread using class in Python for understanding more about creating threads in Python.

 

2. Using Multiprocessing Module:

Here, the process was terminated by using terminate() function which is used to kill a whole process.

import multiprocessing
import time
def process():
  while True:
   for i in range (20):  
    print (i)
    time.sleep(0.05)
t = multiprocessing.Process(target = process)
t.start()
time.sleep(0.5)
t.terminate()
print("Process terminated")

Output:

0
1
2
3
4
5
6
7
8
9
Process terminated

Leave a Reply

Your email address will not be published.