强制CherryPy子线程

2024-04-19 18:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我希望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的线程一起退出。。。。我能克服吗?在


Tags: 程序httpserver线程enginejandowncherrypy
2条回答

这适用于quickstart

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

In order to support its life-cycle, CherryPy defines a set of common channels that will be published to at various states:

“start”: When the bus is in the “STARTING” state

“main”: Periodically from the CherryPy’s mainloop

“stop”: When the bus is in the “STOPPING” state

“graceful”: When the bus requests a reload of subscribers

“exit”: When the bus is in the “EXITING” state

This channel will be published to by the engine automatically. Register therefore any subscribers that would need to react to the transition changes of the engine.

。。在

In order to work with the bus, the implementation provides the following simple API:

cherrypy.engine.publish(channel, *args):

The channel parameter is a string identifying the channel to which the message should be sent to

*args is the message and may contain any valid Python values or objects.

cherrypy.engine.subscribe(channel, callable):

The channel parameter is a string identifying the channel the callable will be registered to.

callable is a Python function or method which signature must match what will be published.

您需要编写停止线程的代码,并将其注册为“stop”事件的侦听器:

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。在

相关问题 更多 >