Skip to content

Instantly share code, notes, and snippets.

@zhangkaizhao
Created February 9, 2017 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangkaizhao/3f2aeb9f974a7d587e621e3ade57700a to your computer and use it in GitHub Desktop.
Save zhangkaizhao/3f2aeb9f974a7d587e621e3ade57700a to your computer and use it in GitHub Desktop.
Test Tornado's periodic callback
import time
import tornado.gen
import tornado.ioloop
class Counter:
n = 0
@tornado.gen.coroutine
def consume(n):
for i in range(1, 17):
print('counter: {0}, number: {1}'.format(n, i))
st = time.time()
yield tornado.gen.sleep(1)
# tornado.gen.sleep(1)
et = time.time()
# print('{0}'.format(et - st))
if i == 1:
print('first time: {0}'.format(st))
elif i == 16:
print('last time: {0}'.format(et))
def fn():
st = time.time()
print('start time: {0}'.format(st))
n = Counter.n
n = n + 1
Counter.n = n
consume(n)
et = time.time()
print('end time: {0}'.format(et))
print('time consumed: {0}'.format(et - st))
if __name__ == '__main__':
main_ioloop = tornado.ioloop.IOLoop.current()
interval_ms = 10 * 1000 # 10 second
scheduler = tornado.ioloop.PeriodicCallback(fn, interval_ms, io_loop=main_ioloop)
scheduler.start()
main_ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment