【问题标题】:stopping a cherrypy server over http通过http停止一个cherrypy服务器
【发布时间】:2011-01-08 16:06:40
【问题描述】:

我有一个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吗?

【问题讨论】:

    标签: python cherrypy


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我使用 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", {})
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-10
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多