Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- 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.
- 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)
- 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)
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement