的CherryPy的服務器啓動這樣的:如何殺死鍵盤上的一個子線程中斷
root = Root()
cherrypy.engine.loggingPlugin = LoggerPlugin(cherrypy.engine)
cherrypy.engine.loggingPlugin.init(root.grapher.writequeue)
cherrypy.engine.loggingPlugin.subscribe()
cherrypy.quickstart(Root(), "/", conf)
LoggerPlugin被定義爲:
class LoggerPlugin(plugins.SimplePlugin):
"""Intended to catch the stop signal and terminate WriteLog"""
def init(self, queue):
self.logger = WriteLog("viewerlog.log", queue, 2)
def start(self):
self.logger.start()
def stop(self):
print "Exit"
self.logger.stop()
print "Exited"
最後,WRITELOG是:
class WriteLog (threading.Thread):
def __init__(self, filename, queue, freq):
self.out = open(filename, "a+")
self.queue = queue
self.freq = freq # The time to sleep after every empty queue, in seconds
self.exit = False
super(WriteLog, self).__init__()
def stop(self):
self.exit = True
def run(self):
while True:
if self.exit:
sys.exit(0)
""" do stuff """
當我按下Ctrl + C時,控制檯看起來像:
ENGINE Bus STOPPING
ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer shut down
ENGINE Stopped thread '_TimeoutMonitor'.
ENGINE Bus STOPPED
ENGINE Bus EXITING
ENGINE Bus EXITED
ENGINE Waiting for child threads to terminate...
ENGINE Waiting for thread Thread-1.
之後沒有任何反應。
由於WriteLog是唯一產生的線程,它應該是罪魁禍首。即使在WriteLog.exit設置爲True時應該調用sys.exit(),也不會發生這種情況。
你可以顯示grapher的代碼嗎? –