Advertisement
Guest User

How do I stop Tornado web server

a guest
Mar 24th, 2012
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. import tornado.ioloop
  2. import tornado.web
  3.  
  4. class MainHandler(tornado.web.RequestHandler):
  5. def get(self):
  6. self.write("Hello, world")
  7.  
  8. application = tornado.web.Application([
  9. (r"/", MainHandler),
  10. ])
  11.  
  12. if __name__ == "__main__":
  13. application.listen(8888)
  14. tornado.ioloop.IOLoop.instance().start()
  15.  
  16. To use asynchronous methods from otherwise-synchronous code (such as
  17. unit tests), you can start and stop the event loop like this:
  18. ioloop = IOLoop()
  19. async_method(ioloop=ioloop, callback=ioloop.stop)
  20. ioloop.start()
  21. ioloop.start() will return after async_method has run its callback,
  22. whether that callback was invoked before or after ioloop.start.
  23.  
  24. class Server:
  25. def __init__(self, port=8888):
  26. self.application = tornado.web.Application([ (r"/", Handler) ])
  27.  
  28. def server_thread(application, port):
  29. http_server = tornado.httpserver.HTTPServer(application)
  30. http_server.listen(port)
  31. tornado.ioloop.IOLoop.instance().start()
  32.  
  33. self.process = Process(target=server_thread,
  34. args=(self.application, port,))
  35.  
  36. def start(self):
  37. self.process.start()
  38.  
  39. def stop(self):
  40. ioloop = tornado.ioloop.IOLoop.instance()
  41. ioloop.add_callback(ioloop.stop)
  42.  
  43. def setup_method(self, _function):
  44. self.server = Server()
  45. self.server.start()
  46. time.sleep(0.5) # Wait for web server to start
  47.  
  48. def teardown_method(self, _function):
  49. self.kstore.stop()
  50. time.sleep(0.5)
  51.  
  52. import tornado.ioloop, tornado.web
  53.  
  54. class Handler(tornado.web.RequestHandler):
  55. def get(self):
  56. self.write("Hello, world")
  57.  
  58. application = tornado.web.Application([ (r"/", Handler) ])
  59.  
  60. def startTornado():
  61. application.listen(8888)
  62. tornado.ioloop.IOLoop.instance().start()
  63.  
  64. def stopTornado():
  65. tornado.ioloop.IOLoop.instance().stop()
  66.  
  67. if __name__ == "__main__":
  68. import time, threading
  69. threading.Thread(target=startTornado).start()
  70. print "Your web server will self destruct in 2 minutes"
  71. time.sleep(120)
  72. stopTornado()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement