Skip to content

Instantly share code, notes, and snippets.

@Cougar
Created January 31, 2016 21:29
Show Gist options
  • Save Cougar/900e2611507bef55da12 to your computer and use it in GitHub Desktop.
Save Cougar/900e2611507bef55da12 to your computer and use it in GitHub Desktop.
Add run_now() method to tornado.ioloop.PeriodicCallback
import tornado.ioloop
class PeriodicCallback(tornado.ioloop.PeriodicCallback):
def run_now(self):
self.stop()
self._running = True
try:
return self.callback()
except Exception:
self.io_loop.handle_callback_exception(self.callback)
finally:
self.start()
import tornado.ioloop
from periodiccallback import PeriodicCallback
import time
def cb():
print("CB called at ", time.ctime())
def cb2():
print("call PeriodicCallback immediately")
pc.run_now()
pc = PeriodicCallback(cb, 3000)
pc.start() # run CB every 3 sec
PeriodicCallback(cb2, 10000).start() # run it immediately every 10 sec
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment