6

我有一个cherrypy 应用程序,我通过wxpython ui 控制http。我想在 ui 关闭时杀死服务器,但我不知道该怎么做。现在我只是在窗口关闭事件上做一个 sys.exit() 但这导致

Traceback (most recent call last):
  File "ui.py", line 67, in exitevent
    urllib.urlopen("http://"+server+"/?sigkill=1")
  File "c:\python26\lib\urllib.py", line 87, in urlopen
    return opener.open(url)
  File "c:\python26\lib\urllib.py", line 206, in open
    return getattr(self, name)(url)
  File "c:\python26\lib\urllib.py", line 354, in open_http
    'got a bad status line', None)
IOError: ('http protocol error', 0, 'got a bad status line', None)

那是因为我没有正确停止cherrypy吗?

4

2 回答 2

10

你是如何停止 CherryPy 的?通过向自身发送 SIGKILL?您至少应该发送 TERM,但更好的是调用cherrypy.engine.exit()(版本 3.1+)。这两种技术都将允许 CherryPy 更优雅地关闭,其中包括允许任何进程内请求(如您的“?sigkill=1”请求本身)完成并干净地关闭。

于 2010-01-25T05:47:14.337 回答
3

我使用 os._exit。我还把它放在一个线程上,这样我就可以在退出之前提供一个“你已经退出服务器”页面。

class MyApp(object):
    @cherrypy.expose
    def exit(self):
        """
        /exit
        Quits the application
        """

        threading.Timer(1, lambda: os._exit(0)).start()
        return render("exit.html", {})
于 2010-03-23T07:47:55.663 回答