2010-01-05 49 views
9

嗯,我希望cherrypy殺死所有子線程上的自動重新加載而不是「等待子線程終止」,因爲我的程序有它自己的線程,我不知道如何通過這個。 CherryPy的懸垂挺括這條線,我不知道該怎麼做才能讓「子線程」終止......強制CherryPy的兒童線程

`

[05/Jan/2010:01:14:24] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('127.0.0.1', 8080)) shut down 
[05/Jan/2010:01:14:24] ENGINE Stopped thread '_TimeoutMonitor'. 
[05/Jan/2010:01:14:24] ENGINE Bus STOPPED 
[05/Jan/2010:01:14:24] ENGINE Bus EXITING 
[05/Jan/2010:01:14:24] ENGINE Bus EXITED 
[05/Jan/2010:01:14:05] ENGINE Waiting for child threads to terminate... 

`

它永遠不會繼續..所以我想強制子線程關閉...

我知道這是因爲我的應用程序正在使用它自己的線程,我想cherrypy希望這些線程與CherryPy的一起退出....我可以克服這一點?

+0

我開始覺得我應該超載CherryPy的自動重載殺死我自己client.thread ......但如何,我不KNO。 – user233864 2010-01-05 06:46:43

回答

11

你需要編寫停止你的線程代碼,並將其註冊爲一個偵聽器「停止」事件:

from cherrypy.process import plugins 

class MyFeature(plugins.SimplePlugin): 
    """A feature that does something.""" 

    def start(self): 
     self.bus.log("Starting my feature") 
     self.threads = mylib.start_new_threads() 

    def stop(self): 
     self.bus.log("Stopping my feature.") 
     for t in self.threads: 
      mylib.stop_thread(t) 
      t.join() 

my_feature = MyFeature(cherrypy.engine) 
my_feature.subscribe() 

詳情請參閱http://www.cherrypy.org/wiki/BuiltinPluginshttp://www.cherrypy.org/wiki/CustomPlugins

+1

好的。我會研究這一點。我正在使用快速入門方法。我可以將這些啓動和停止方法放入我用於cherrypy.quickstart()的根類中嗎?或者你可以告訴我,我將如何使用這個類MyFeature(),我已經使用Root類的根類。對不起,我還沒有廣泛使用CherryPy .. – user233864 2010-01-06 03:01:29

+2

當然;你可以把代碼放在你喜歡的任何地方;唯一重要的是您在運行快速入門之前訂閱它。 – fumanchu 2010-01-11 18:50:15

-1

這適用於快速入門

def stopit(): 
    print 'stop handler invoked' 
    #... 
stopit.priority = 10 
cherrypy.engine.subscribe('stop', stopit)