gpt4 book ai didi

python - 如何停止(并重启!)Tornado 服务器?

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:54 27 4
gpt4 key购买 nike

我希望能够停止和重新启动 Tornado 服务器以进行测试和演示。但是好像没有释放端口。

以下代码基于answer showing how to properly stop Tornado .我刚刚在底部添加了尝试重启 Tornado 的代码。它因“错误:地址正在使用”异常而失败。我什至添加了对 ioloop.close() 的调用,但这没有帮助。

#! /usr/bin/env python

import threading
import tornado.ioloop
import tornado.web
import time


class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world!\n")

def start_tornado(*args, **kwargs):
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8888)
print "Starting Torando"
tornado.ioloop.IOLoop.instance().start()
print "Tornado finished"

def stop_tornado():
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(ioloop.stop)
ioloop.add_callback(ioloop.close) # I added this but it didn't help.
print "Asked Tornado to exit"

def main():

t = threading.Thread(target=start_tornado)
t.start()

time.sleep(1)

stop_tornado()
t.join()
print "Tornado thread stopped."

t = threading.Thread(target=start_tornado) # Attempt restart.
t.start()

if __name__ == "__main__":
main()

输出:

Starting Torando
Asked Tornado to exit
Tornado finished
Tornado thread stopped.
Exception in thread Thread-2:
Traceback (most recent call last):
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "./test_tonado_restart.py", line 17, in start_tornado
application.listen(8888)
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/site-packages/tornado/web.py", line 1825, in listen
server.listen(port, address)
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/site-packages/tornado/tcpserver.py", line 126, in listen
sockets = bind_sockets(port, address=address)
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/site-packages/tornado/netutil.py", line 196, in bind_sockets
sock.bind(sockaddr)
File "/home/mudd/musl/Python-2.7.11.install/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 98] Address in use

最佳答案

除非您确实需要,否则不要使用这样的线程 - 它们会使事情复杂化。对于测试,请使用 tornado.testing.AsyncTestCaseAsyncHTTPTestCase

要释放端口,您需要停止HTTPServer,而不仅仅是IOLoop。事实上,您甚至可能根本不需要停止 IOLoop。 (但通常我会通过让进程退出并从头重新启动来重新启动一切)。

您的示例的非线程版本类似于:

#! /usr/bin/env python

import tornado.ioloop
import tornado.web
import time


class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world!\n")

def start_app(*args, **kwargs):
application = tornado.web.Application([
(r"/", MainHandler),
])
server = application.listen(8888)
print "Starting app"
return server

def stop_tornado():
ioloop = tornado.ioloop.IOLoop.current()
ioloop.add_callback(ioloop.stop)
print "Asked Tornado to exit"

def main():
server = start_app()
tornado.ioloop.IOLoop.current().add_timeout(
datetime.timedelta(seconds=1),
stop_tornado)
tornado.ioloop.IOLoop.current().start()
print "Tornado finished"
server.stop()

# Starting over
start_app()
tornado.ioloop.IOLoop.current().start()

关于python - 如何停止(并重启!)Tornado 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com