考虑一个假设的线程 Python 应用程序,它在无限循环中运行每个线程:
import signal
import sys
import threading
import time
class CallSomebody (threading.Thread):
def __init__(self, target, *args):
self._target = target
self._args = args
threading.Thread.__init__(self)
def run (self):
self._target(*self._args)
def call (who):
while True:
print "Who you gonna call? %s" % (str(who))
def signal_handler(signal, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
a=CallSomebody(call, 'Ghostbusters!')
a.daemon=True
b=CallSomebody(call, 'The Exorcist!')
b.daemon=True
a.start()
b.start()
a.join()
b.join()
运行应用程序时,通过按下发送 SIGINTCtrlC不会停止应用程序。我尝试删除这些daemon
陈述,但这没有帮助。我缺少什么基本思想?
谢谢。