2010-01-05 48 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)