如果它是一个线程,你可以使用低级线程(或Python 3中的_thread)模块通过调用thread.exit()来杀死具有异常的线程.
> thread.exit():提高SystemExit异常.什么时候没抓到
这将导致线程以静默方式退出.
一个更干净的方法(取决于你的处理设置如何)将指示线程停止处理并使用实例变量退出,然后从主线程调用join()方法等待线程退出.
例:
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
self._stop_req = False
def run(self):
while not self._stop_req:
pass
# processing
# clean up before exiting
def stop(self):
# triggers the threading event
self._stop_req = True;
def main():
# set up the processing thread
processing_thread = MyThread()
processing_thread.start()
# do other things
# stop the thread and wait for it to exit
processing_thread.stop()
processing_thread.join()
if __name__ == "__main__":
main()