Last active
December 13, 2016 16:47
-
-
Save NightBlues/c2427e622e4ff0f84a08 to your computer and use it in GitHub Desktop.
tornado_gracefull_stop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parallel --no-notice curl localhost:8080/{} ::: 4 5 3 8 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
import tornado.web | |
import tornado.gen | |
class MyHandler(tornado.web.RequestHandler): | |
@tornado.gen.coroutine | |
def get(self, num): | |
yield tornado.gen.sleep(int(num)) | |
self.write('res {}\n'.format(num)) | |
def stop(loop, server): | |
def soft_stop(): | |
if set(loop._handlers.keys()) - set([loop._waker.fileno()]): | |
loop.call_later(1, soft_stop) | |
else: | |
loop.stop() | |
print 'Stopping server, {}'.format(loop._handlers) | |
server.stop() | |
server.close_all_connections() | |
#class GracefulRequestHandler(tornado.web.RequestHandler): | |
# def initialize(self): | |
# if self.application.is_shutting_down: | |
# self.set_header('Connection', 'close') | |
# self.request.connection.no_keep_alive = True | |
soft_stop() | |
loop.call_later(30, loop.stop) | |
if __name__ == '__main__': | |
loop = tornado.ioloop.IOLoop.current() | |
app = tornado.web.Application([(r'/(.*)', MyHandler)]) | |
server = app.listen(8080, '127.0.0.1') | |
loop.call_later(5, stop, loop, server) | |
loop.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment