lwzm
12/7/2013 - 3:23 PM

make _Timeout(tornado.ioloop) visible

make _Timeout(tornado.ioloop) visible

import datetime
import random
import time

import tornado.ioloop
import tornado.web

TIME = "%Y-%m-%d %H:%M:%S"

# patch
tornado.ioloop._Timeout.__repr__ = lambda self: "{} ({})  {}".format(
    time.strftime(TIME, time.localtime(self.deadline)),
    self.deadline,
    self.callback and self.callback.__doc__,
    )

io_loop = tornado.ioloop.IOLoop.instance()

f = lambda: print(time.time(), time.strftime(TIME))

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        t = time.time()
        io_loop.add_timeout(
            datetime.timedelta(seconds=random.random()*60),
            f).callback.__doc__ = "XXX, {} ({})".format(
                time.strftime(TIME, time.localtime(t)), t)

        self.set_header("Content-Type", "text/plain")
        self.write("\n".join(map(repr, sorted(io_loop._timeouts))))

application = tornado.web.Application([
    (r"/", MainHandler),
])


if __name__ == "__main__":
    import tornado.options
    tornado.options.parse_command_line()
    application.listen(1024)
    io_loop.start()