如何停止 Tornado Web 服务器? [英] How do I stop Tornado web server?
问题描述
我一直在玩 Tornado 网络服务器,现在我想停止网络服务器(例如在单元测试期间).以下简单示例存在于 Tornado 网页上:
I've been playing around a bit with the Tornado web server and have come to a point where I want to stop the web server (for example during unit testing). The following simple example exists on the Tornado web page:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
一旦 tornado.ioloop.IOLoop.instance().start()
被调用,它就会阻塞程序(或当前线程).阅读 IOLoop
对象的源代码给出stop
函数文档中的这个例子:
Once tornado.ioloop.IOLoop.instance().start()
is called, it blocks the program (or current thread). Reading the source code for the IOLoop
object gives this example in the documentation for the stop
function:
To use asynchronous methods from otherwise-synchronous code (such as
unit tests), you can start and stop the event loop like this:
ioloop = IOLoop()
async_method(ioloop=ioloop, callback=ioloop.stop)
ioloop.start()
ioloop.start() will return after async_method has run its callback,
whether that callback was invoked before or after ioloop.start.
但是,我不知道如何将其集成到我的程序中.我实际上有一个封装 web 服务器的类(有它自己的 start
和 stop
函数),但是一旦我调用 start,程序(或测试)就会无论如何都要阻止.
However, I have no idea how to integrate this into my program. I actually have a class that encapsulates the web server (having it's own start
and stop
functions), but as soon as I call start, the program (or tests) will of course block anyway.
我尝试在另一个进程中启动 Web 服务器(使用 multiprocessing
包).这是包装 Web 服务器的类:
I've tried to start the web server in another process (using the multiprocessing
package). This is the class that is wrapping the web server:
class Server:
def __init__(self, port=8888):
self.application = tornado.web.Application([ (r"/", Handler) ])
def server_thread(application, port):
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
self.process = Process(target=server_thread,
args=(self.application, port,))
def start(self):
self.process.start()
def stop(self):
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(ioloop.stop)
然而,stop 似乎并没有完全停止 Web 服务器,因为它在下一个测试中仍在运行,即使使用这个测试设置:
However, stop does not seem to entirely stop the web server since it is still running in the next test, even with this test setup:
def setup_method(self, _function):
self.server = Server()
self.server.start()
time.sleep(0.5) # Wait for web server to start
def teardown_method(self, _function):
self.kstore.stop()
time.sleep(0.5)
如何在 Python 程序中启动和停止 Tornado Web 服务器?
How can I start and stop a Tornado web server from within a Python program?
推荐答案
我刚刚遇到这个问题并自己发现了这个问题,并使用该线程中的信息提出了以下内容.我只是将我的工作独立 Tornado 代码(从所有示例复制)并将实际的起始代码移动到一个函数中.然后我将该函数称为线程线程.我的情况不同,因为线程调用是从我现有的代码中完成的,我刚刚导入了 startTornado 和 stopTornado 例程.
I just ran into this and found this issue myself, and using info from this thread came up with the following. I simply took my working stand alone Tornado code (copied from all the examples) and moved the actual starting code into a function. I then called the function as a threading thread. My case different as the threading call was done from my existing code where I just imported the startTornado and stopTornado routines.
上面的建议似乎很有效,所以我想我会提供缺少的示例代码.我在 FC16 系统上的 Linux 下测试了这段代码(并修复了我最初的 type-o).
The suggestion above seemed to work great, so I figured I would supply the missing example code. I tested this code under Linux on a FC16 system (and fixed my initial type-o).
import tornado.ioloop, tornado.web
class Handler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([ (r"/", Handler) ])
def startTornado():
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
def stopTornado():
tornado.ioloop.IOLoop.instance().stop()
if __name__ == "__main__":
import time, threading
threading.Thread(target=startTornado).start()
print "Your web server will self destruct in 2 minutes"
time.sleep(120)
stopTornado()
希望这对下一个人有所帮助.
Hope this helps the next person.
这篇关于如何停止 Tornado Web 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!