我试图运行下面的代码(这我简化了一下):如何中断python多线程应用程序?
def RunTests(self):
from threading import Thread
import signal
global keep_running
keep_running = True
signal.signal(signal.SIGINT, stop_running)
for i in range(0, NumThreads):
thread = Thread(target = foo)
self._threads.append(thread)
thread.start()
# wait for all threads to finish
for t in self._threads:
t.join()
def stop_running(signl, frme):
global keep_testing
keep_testing = False
print "Interrupted by the Master. Good by!"
return 0
def foo(self):
global keep_testing
while keep_testing:
DO_SOME_WORK();
我想到的是,用户按下CTRL + C程序将通过信息打印好和中断。但它不起作用。哪里有问题?
感谢
非常感谢。我花了很多时间来解决这个问题。 – 2010-10-28 13:27:45