update longpoll examples

Add a longpoll example for tornado and terminate the lines we send with
a '\n'.
This commit is contained in:
Randall Leeds 2011-11-07 17:14:37 -08:00
parent b955407003
commit f02b0d9cca
2 changed files with 24 additions and 5 deletions

View File

@ -8,13 +8,32 @@
# $ gunicorn -k egg:gunicorn#tornado tornadoapp:app # $ 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): def get(self):
self.write("Hello, world") self.write("Hello, world")
app = tornado.web.Application([ class LongPollHandler(RequestHandler):
(r"/", MainHandler) @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)
]) ])

View File

@ -9,7 +9,7 @@ import time
class TestIter(object): class TestIter(object):
def __iter__(self): def __iter__(self):
lines = ['line 1', 'line 2'] lines = ['line 1\n', 'line 2\n']
for line in lines: for line in lines:
yield line yield line
time.sleep(20) time.sleep(20)