如何在Linux中停止WsgiServer

时间:2012-07-30 07:25:00

标签: linux wsgi cherrypy wsgiserver

我是新手。 我刚刚尝试了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来停止服务器时,它不会停止。怎么做?

1 个答案:

答案 0 :(得分: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()