Software developer specializing in MongoDB, Python, Tornado, and Javascript, with particular interests in real-time web and new tools that get the job done with grace and alacrity. A. Jesse Jiryu is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

Pausing with Tornado a Genator Based Interface

04.23.2012
| 2961 views |
  • submit to reddit
Throwing this in my blog so I don’t forget again. The way to sleep for a certain period of time using tornado.gen is:

import tornado.web
import tornado.ioloop
from tornado import gen

class MyHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
	@gen.engine
    def get(self):
        self.write("sleeping .... ")
        # Do nothing for 5 sec
        yield gen.Task(loop.add_timeout, time.time() + 5)
        self.write("I'm awake!")
        self.finish()

Simple once you see it, but for some reason this has been the hardest for me to get used to.

Published at DZone with permission of A. Jesse Jiryu Davis, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)