2014-04-10 32 views
0

的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(),也不會發生這種情況。

+0

你可以顯示grapher的代碼嗎? –

回答

0

嗯,你使用的是什麼版本的python和cherrypy?我運行了這段代碼,線程正確終止。

import cherrypy 
import sys 
import threading    

class Root(object): 
    def asdf(self): 
     return 'hi' 

    def grapher(self): 
     return 'hi' 

class LoggerPlugin(cherrypy.process.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") 

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): 
     print("WriteLog Exited") 
     self.exit = True 

    def run(self): 
     while True: 
      if self.exit: 
       sys.exit(0) 

      """ do stuff """ 

root = Root() 
cherrypy.engine.loggingPlugin = LoggerPlugin(cherrypy.engine) 
cherrypy.engine.loggingPlugin.init(root.grapher) 
cherrypy.engine.loggingPlugin.subscribe() 
cherrypy.quickstart(Root(), "/") 

這裏是我的輸出...

[[email protected] Scratch_Pad]$ /opt/python3/bin/python3.3 webapp.py 
[10/Apr/2014:08:28:26] ENGINE Listening for SIGTERM. 
[10/Apr/2014:08:28:26] ENGINE Listening for SIGHUP. 
[10/Apr/2014:08:28:26] ENGINE Listening for SIGUSR1. 
[10/Apr/2014:08:28:26] ENGINE Bus STARTING 
CherryPy Checker: 
The Application mounted at '' has an empty config. 

[10/Apr/2014:08:28:26] ENGINE Started monitor thread 'Autoreloader'. 
[10/Apr/2014:08:28:26] ENGINE Started monitor thread '_TimeoutMonitor'. 
[10/Apr/2014:08:28:27] ENGINE Serving on 127.0.0.1:8080 
[10/Apr/2014:08:28:27] ENGINE Bus STARTED 
^C[10/Apr/2014:08:28:29] ENGINE Keyboard Interrupt: shutting down bus 
[10/Apr/2014:08:28:29] ENGINE Bus STOPPING 
[10/Apr/2014:08:28:29] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) shut down 
[10/Apr/2014:08:28:29] ENGINE Stopped thread 'Autoreloader'. 
Exit 
WriteLog Exited 
Exited 
[10/Apr/2014:08:28:29] ENGINE Stopped thread '_TimeoutMonitor'. 
[10/Apr/2014:08:28:29] ENGINE Bus STOPPED 
[10/Apr/2014:08:28:29] ENGINE Bus EXITING 
[10/Apr/2014:08:28:29] ENGINE Bus EXITED 
[10/Apr/2014:08:28:29] ENGINE Waiting for child threads to terminate... 
[10/Apr/2014:08:28:29] ENGINE Waiting for thread Thread-1. 

這裏有消失的過程...

500  5526 5452 99 08:23 pts/1 00:00:42 /opt/python3/bin/python3.3 webapp.py 

相信線程沒有確認它的消失。你還在看這個過程嗎?

希望這會有所幫助!

+0

我在Ubuntu的包服務器和Python 2.7上使用CherryPy。它仍然看起來像線程沒有關閉 - 最後它說「等待線程線程1」。這樣做的問題是它可以防止ENGINE重新啓動或正常結束。 – Sky

+0

即使我做了py更改並保存它,我的服務器停止並正確重啓......「[10/Apr/2014:10:3​​9:30]引擎等待子線程終止... [10/Apr/2014:10:3​​9:30] ENGINE重新產卵/ home/programmer/Aptana Studio 3 Workspace/Scratch_Pad/webapp.py「。所以威脅絕對會消失。 –

+0

它可能是一個Python 2.7的錯誤? :S除了print()函數的區別之外,我的代碼與您的代碼完全相同,但是我的程序在Thread-1等待後掛起。 – Sky