Skip to content

Instantly share code, notes, and snippets.

@lwzm
Last active December 30, 2015 14:39
Show Gist options
  • Save lwzm/7843815 to your computer and use it in GitHub Desktop.
Save lwzm/7843815 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment