Is there any way to kill a Thread in Python?

Answer

The nice way of handling this if you can afford it (if you are managing your own threads) is to have an exit_request flag that each threads checks on regular interval to see if it is time for him to exit.

For example:

import threading

classStoppableThread(threading.Thread):"""Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""def __init__(self):
        super(StoppableThread, self).__init__()
        self._stop = threading.Event()def stop(self):
        self._stop.set()def stopped(self):return self._stop.isSet()

All python Questions

Ask your interview questions on python

Write Your comment or Questions if you want the answers on python from python Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---