【问题标题】:How to stop WsgiServer in Linux如何在 Linux 中停止 WsgiServer
【发布时间】:2012-07-30 07:25:00
【问题描述】:

我是cherrypy的新手。 我刚刚尝试了一个wsgiserver的示例程序。程序如下

from cherrypy import wsgiserver

def my_crazy_app(environ, start_response):
  status = '200 OK'
  response_headers = [('Content-type','text/plain')]
  start_response(status, response_headers)
  return ['Hello world!']

server = wsgiserver.CherryPyWSGIServer(
        ('127.0.0.1', 8080), my_crazy_app,
        server_name='localhost')
server.start()

我成功得到了输出Hello world, 但问题是当我在终端上点击Ctrl-c 停止服务器时,它并没有停止。怎么做?

【问题讨论】:

    标签: linux wsgi cherrypy wsgiserver


    【解决方案1】:

    IIRC,wsgiserver 本身不与任何信号相关联,因此不尊重 SIGINT 中断。 只有更高级别的 CherryPy 引擎才能提供。如果你不能使用它,你可能想要 使用 Python 信号模块安装处理程序。

    嗯,按照这些思路来做就行了:

    import signal
    
    from cherrypy import wsgiserver 
    def my_crazy_app(environ, start_response): 
       status = '200 OK' 
       response_headers = [('Content-type','text/plain')] 
       start_response(status, response_headers) 
       return ['Hello world!'] 
    
    server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8080), my_crazy_app, server_name='localhost')
    
    def stop_server(*args, **kwargs):
      server.stop()
    
    signal.signal(signal.SIGINT,  stop_server)
    
    server.start()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      • 2016-01-02
      相关资源
      最近更新 更多