How do I stop Tornado web server?

后端 未结 9 711
渐次进展
渐次进展 2020-12-08 06:48

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 sim

9条回答
  •  一个人的身影
    2020-12-08 07:37

    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.

    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.

提交回复
热议问题