Last active
December 31, 2015 05:49
-
-
Save mayflaver/7943971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tornado.ioloop | |
from tornado.ioloop import IOLoop | |
import tornado.web | |
import tornado.gen | |
import tornado.httpclient | |
from resolver import CaresResolver | |
import time | |
from functools import partial | |
from period import PeriodicCallback | |
class MainHandler(tornado.web.RequestHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
for url in ['http://www.baidu.com']: | |
self.url = url | |
print self.url | |
self._count = 5 | |
self._period = PeriodicCallback(self.test, 1000) | |
self._period.start() | |
@tornado.gen.coroutine | |
def test(self): | |
print self.url | |
print self._count | |
if self._count <= 0: | |
self._period.stop() | |
else: | |
self._count -= 1 | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
if __name__ == "__main__": | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tornado.ioloop import IOLoop | |
from functools import partial | |
from tornado.gen import coroutine | |
class PeriodicCallback(object): | |
"""Schedules the given callback to be called periodically. | |
The callback is called every ``callback_time`` milliseconds. | |
`start` must be called after the `PeriodicCallback` is created. | |
""" | |
def __init__(self, callback, callback_time, io_loop=None): | |
self.callback = callback | |
if callback_time <= 0: | |
raise ValueError("Periodic callback must have a positive callback_time") | |
self.callback_time = callback_time | |
self.io_loop = io_loop or IOLoop.current() | |
self._running = False | |
self._timeout = None | |
self._count = 10 | |
def start(self): | |
"""Starts the timer.""" | |
self._running = True | |
self._next_timeout = self.io_loop.time() | |
self._schedule_next() | |
def stop(self): | |
"""Stops the timer.""" | |
self._running = False | |
if self._timeout is not None: | |
self.io_loop.remove_timeout(self._timeout) | |
self._timeout = None | |
@coroutine | |
def _run(self): | |
if not self._running: | |
return | |
try: | |
self.callback() | |
except Exception: | |
self.io_loop.handle_callback_exception(self.callback) | |
self._schedule_next() | |
def _schedule_next(self): | |
if self._running: | |
current_time = self.io_loop.time() | |
while self._next_timeout <= current_time: | |
self._next_timeout += self.callback_time / 1000.0 | |
self._timeout = self.io_loop.add_timeout(self._next_timeout, self._run) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment