Skip to content

Instantly share code, notes, and snippets.

@arnaudsj
Forked from sris/polling_example.py
Created December 10, 2009 03:26
Show Gist options
  • Save arnaudsj/253088 to your computer and use it in GitHub Desktop.
Save arnaudsj/253088 to your computer and use it in GitHub Desktop.
import logging
import random
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
import tornado.ioloop
self.scheduler = tornado.ioloop.PeriodicCallback(self._check, 1e3)
self.scheduler.start()
def _check(self):
if self._are_where_there_yet():
self.scheduler.stop()
self.write("Hello, world")
self.finish()
def _are_where_there_yet(self):
n = random.randint(1,10)
logging.info("current value: %s", n)
return n == 5
def main():
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment