Skip to content

Commit

Permalink
Update tornadoapp.py
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc authored Aug 6, 2024
1 parent 7f55988 commit 5e39f88
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions examples/frameworks/tornadoapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,23 @@
# $ gunicorn -k tornado tornadoapp:app
#

from datetime import timedelta
import tornado.ioloop
import tornado.web
from tornado import gen

from tornado.web import Application, RequestHandler, asynchronous
from tornado.ioloop import IOLoop

class MainHandler(RequestHandler):
def get(self):
self.write("Hello, world")

class LongPollHandler(RequestHandler):
@asynchronous
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
lines = ['line 1\n', 'line 2\n']
# Your asynchronous code here
yield gen.sleep(1) # Example of an asynchronous operation
self.write("Hello, World!")

def send():
try:
self.write(lines.pop(0))
self.flush()
except:
self.finish()
else:
IOLoop.instance().add_timeout(timedelta(0, 20), send)
send()
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])

app = Application([
(r"/", MainHandler),
(r"/longpoll", LongPollHandler)
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()

0 comments on commit 5e39f88

Please sign in to comment.