我正在尝试捕获cntrl-c事件,以便我可以优雅地关闭我的服务器(使用瓶子框架)。但是,当我测试这个时,我从未在调试器中点击stop方法,我也得到了一个未闭合的套接字警告以确认这一点。我在这里错过了什么。
class CFMServer(ServerAdapter):
def run(self, handler):
from wsgiref.simple_server import make_server, WSGIRequestHandler
self.server = make_server(self.host, self.port, handler, **self.options)
# set signal handlers
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
try:
self.server.serve_forever()
except KeyboardInterrupt:
self.stop()
raise
def stop(self):
pdb.set_trace()
manager.save()
self.server.server_close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('port', metavar='P', type=int,
help='Port to assign the server in localhost')
args = parser.parse_args()
port = args.port
server = CFMServer(host='localhost', port=port)
service.run(server=server, debug=True)
当我中断时,这是shell的输出。
Bottle v0.13-dev server starting up (using CFMServer())...
Listening on http://localhost:11245/
Hit Ctrl-C to quit.
^CTraceback (most recent call last):
File "./cfm-service", line 127, in <module>
service.run(server=server, debug=True)
File "/repo/ekrmann/next-debugger/python/emca/gdbcmds/emca/core/third_party/bottle.py", line 881, in run
run(self, **kwargs)
File "/repo/ekrmann/next-debugger/python/emca/gdbcmds/emca/core/third_party/bottle.py", line 3476, in run
server.run(app)
File "./cfm-service", line 108, in run
self.server.serve_forever()
File "/app/vbuild/RHEL6-i686/python/3.5.0/lib/python3.5/socketserver.py", line 237, in serve_forever
ready = selector.select(poll_interval)
File "/app/vbuild/RHEL6-i686/python/3.5.0/lib/python3.5/selectors.py", line 367, in select
fd_event_list = self._poll.poll(timeout)
TypeError: stop() takes 1 positional argument but 3 were given
sys:1: ResourceWarning: unclosed <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 11245)>
答案 0 :(得分:1)
您的stop
方法未实现Python信号处理程序签名。而不是
signal.signal(signal.SIGINT, self.stop)
编写一个辅助函数:
signal.signal(signal.SIGINT, lambda _signum, _stack: self.stop())