diff --git a/examples/frameworks/tornadoapp.py b/examples/frameworks/tornadoapp.py index 6c6781ce..2f76625f 100644 --- a/examples/frameworks/tornadoapp.py +++ b/examples/frameworks/tornadoapp.py @@ -8,13 +8,32 @@ # $ gunicorn -k egg:gunicorn#tornado tornadoapp:app # -import tornado.web +from datetime import timedelta -class MainHandler(tornado.web.RequestHandler): +from tornado.web import Application, RequestHandler, asynchronous +from tornado.ioloop import IOLoop + +class MainHandler(RequestHandler): def get(self): self.write("Hello, world") -app = tornado.web.Application([ - (r"/", MainHandler) +class LongPollHandler(RequestHandler): + @asynchronous + def get(self): + lines = ['line 1\n', 'line 2\n'] + + def send(): + try: + self.write(lines.pop(0)) + self.flush() + except: + self.finish() + else: + IOLoop.instance().add_timeout(timedelta(0, 20), send) + send() + +app = Application([ + (r"/", MainHandler), + (r"/longpoll", LongPollHandler) ]) diff --git a/examples/longpoll.py b/examples/longpoll.py index fc1d6f18..0bbbb283 100644 --- a/examples/longpoll.py +++ b/examples/longpoll.py @@ -9,7 +9,7 @@ import time class TestIter(object): def __iter__(self): - lines = ['line 1', 'line 2'] + lines = ['line 1\n', 'line 2\n'] for line in lines: yield line time.sleep(20)