Python tornado.ioloop.PeriodicCallback() Examples
The following are 30
code examples of tornado.ioloop.PeriodicCallback().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tornado.ioloop
, or try the search function
.
Example #1
Source File: ioloop_test.py From tornado-zh with MIT License | 8 votes |
def test_overrun(self): sleep_durations = [9, 9, 10, 11, 20, 20, 35, 35, 0, 0] expected = [ 1010, 1020, 1030, # first 3 calls on schedule 1050, 1070, # next 2 delayed one cycle 1100, 1130, # next 2 delayed 2 cycles 1170, 1210, # next 2 delayed 3 cycles 1220, 1230, # then back on schedule. ] calls = [] def cb(): calls.append(self.io_loop.time()) if not sleep_durations: self.io_loop.stop() return self.io_loop.sleep(sleep_durations.pop(0)) pc = PeriodicCallback(cb, 10000) pc.start() self.io_loop.start() self.assertEqual(calls, expected)
Example #2
Source File: ioloop_test.py From teleport with Apache License 2.0 | 6 votes |
def test_clock_backwards(self): pc = PeriodicCallback(None, 10000) # Backwards jumps are ignored, potentially resulting in a # slightly slow schedule (although we assume that when # time.time() and time.monotonic() are different, time.time() # is getting adjusted by NTP and is therefore more accurate) self.assertEqual(self.simulate_calls(pc, [-2, -1, -3, -2, 0]), [1010, 1020, 1030, 1040, 1050]) # For big jumps, we should perhaps alter the schedule, but we # don't currently. This trace shows that we run callbacks # every 10s of time.time(), but the first and second calls are # 110s of real time apart because the backwards jump is # ignored. self.assertEqual(self.simulate_calls(pc, [-100, 0, 0]), [1010, 1020, 1030])
Example #3
Source File: tornado.py From thrift_connector with MIT License | 6 votes |
def __init__(self, service, host, port, timeout=30, name=None, raise_empty=False, max_conn=30, connection_class=ThriftClient, keepalive=None, tracking=False, tracker_factory=None, use_limit=None, check_interval=10): super(TornadoHeartbeatClientPool, self).__init__( service=service, host=host, port=port, timeout=timeout, name=name, raise_empty=raise_empty, max_conn=max_conn, connection_class=connection_class, keepalive=keepalive, tracking=tracking, tracker_factory=tracker_factory, use_limit=use_limit, ) self.check_interval = check_interval self._heartbeat_timer = PeriodicCallback( self.maintain_connections, max(1, self.timeout - 5) * 1000) self._heartbeat_timer.start()
Example #4
Source File: ioloop_test.py From pySINDy with MIT License | 6 votes |
def test_clock_backwards(self): pc = PeriodicCallback(None, 10000) # Backwards jumps are ignored, potentially resulting in a # slightly slow schedule (although we assume that when # time.time() and time.monotonic() are different, time.time() # is getting adjusted by NTP and is therefore more accurate) self.assertEqual(self.simulate_calls(pc, [-2, -1, -3, -2, 0]), [1010, 1020, 1030, 1040, 1050]) # For big jumps, we should perhaps alter the schedule, but we # don't currently. This trace shows that we run callbacks # every 10s of time.time(), but the first and second calls are # 110s of real time apart because the backwards jump is # ignored. self.assertEqual(self.simulate_calls(pc, [-100, 0, 0]), [1010, 1020, 1030])
Example #5
Source File: autoreload.py From pySINDy with MIT License | 6 votes |
def start(check_time=500): """Begins watching source files for changes. .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed. """ io_loop = ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") modify_times = {} callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time) scheduler.start()
Example #6
Source File: autoreload.py From opendevops with GNU General Public License v3.0 | 6 votes |
def start(check_time: int = 500) -> None: """Begins watching source files for changes. .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed. """ io_loop = ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") modify_times = {} # type: Dict[str, float] callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time) scheduler.start()
Example #7
Source File: ioloop_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
def simulate_calls(self, pc, durations): """Simulate a series of calls to the PeriodicCallback. Pass a list of call durations in seconds (negative values work to simulate clock adjustments during the call, or more or less equivalently, between calls). This method returns the times at which each call would be made. """ calls = [] now = 1000 pc._next_timeout = now for d in durations: pc._update_next(now) calls.append(pc._next_timeout) now = pc._next_timeout + d return calls
Example #8
Source File: core.py From jupyter_environment_kernels with BSD 2-Clause "Simplified" License | 6 votes |
def __init__(self, *args, **kwargs): super(EnvironmentKernelSpecManager, self).__init__(*args, **kwargs) self.log.info("Using EnvironmentKernelSpecManager...") self._env_data_cache = {} if self.refresh_interval > 0: try: from tornado.ioloop import PeriodicCallback, IOLoop # Initial loading NOW IOLoop.current().call_later(0, callback=self._update_env_data, initial=True) # Later updates updater = PeriodicCallback(callback=self._update_env_data, callback_time=1000 * 60 * self.refresh_interval) updater.start() if not updater.is_running(): raise Exception() self._periodic_updater = updater self.log.info("Started periodic updates of the kernel list (every %s minutes).", self.refresh_interval) except: self.log.exception("Error while trying to enable periodic updates of the kernel list.") else: self.log.info("Periodical updates the kernel list are DISABLED.")
Example #9
Source File: ioloop_test.py From tornado-zh with MIT License | 6 votes |
def test_overrun(self): sleep_durations = [9, 9, 10, 11, 20, 20, 35, 35, 0, 0] expected = [ 1010, 1020, 1030, # first 3 calls on schedule 1050, 1070, # next 2 delayed one cycle 1100, 1130, # next 2 delayed 2 cycles 1170, 1210, # next 2 delayed 3 cycles 1220, 1230, # then back on schedule. ] calls = [] def cb(): calls.append(self.io_loop.time()) if not sleep_durations: self.io_loop.stop() return self.io_loop.sleep(sleep_durations.pop(0)) pc = PeriodicCallback(cb, 10000) pc.start() self.io_loop.start() self.assertEqual(calls, expected)
Example #10
Source File: autoreload.py From tornado-zh with MIT License | 6 votes |
def start(io_loop=None, check_time=500): """Begins watching source files for changes. .. versionchanged:: 4.1 The ``io_loop`` argument is deprecated. """ io_loop = io_loop or ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") if _has_execv: add_reload_hook(functools.partial(io_loop.close, all_fds=True)) modify_times = {} callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop) scheduler.start()
Example #11
Source File: throttler.py From jaeger-client-python with Apache License 2.0 | 6 votes |
def _delayed_polling(self): def callback(): self._fetch_credits(self._operations()) periodic = PeriodicCallback( callback=callback, # convert interval to milliseconds callback_time=self.refresh_interval * 1000) self._fetch_credits(self._operations()) with self.lock: if not self.running: return self.periodic = periodic self.periodic.start() self.logger.info( 'Throttling client started with refresh interval %d sec', self.refresh_interval)
Example #12
Source File: ioloop_test.py From pySINDy with MIT License | 6 votes |
def simulate_calls(self, pc, durations): """Simulate a series of calls to the PeriodicCallback. Pass a list of call durations in seconds (negative values work to simulate clock adjustments during the call, or more or less equivalently, between calls). This method returns the times at which each call would be made. """ calls = [] now = 1000 pc._next_timeout = now for d in durations: pc._update_next(now) calls.append(pc._next_timeout) now = pc._next_timeout + d return calls
Example #13
Source File: autoreload.py From tornado-zh with MIT License | 6 votes |
def start(io_loop=None, check_time=500): """Begins watching source files for changes. .. versionchanged:: 4.1 The ``io_loop`` argument is deprecated. """ io_loop = io_loop or ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") if _has_execv: add_reload_hook(functools.partial(io_loop.close, all_fds=True)) modify_times = {} callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop) scheduler.start()
Example #14
Source File: autoreload.py From zulip with Apache License 2.0 | 6 votes |
def start(io_loop=None, check_time=500): """Begins watching source files for changes. .. versionchanged:: 4.1 The ``io_loop`` argument is deprecated. """ io_loop = io_loop or ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") modify_times = {} callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop) scheduler.start()
Example #15
Source File: ioloop_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
def test_clock_backwards(self): pc = PeriodicCallback(self.dummy, 10000) # Backwards jumps are ignored, potentially resulting in a # slightly slow schedule (although we assume that when # time.time() and time.monotonic() are different, time.time() # is getting adjusted by NTP and is therefore more accurate) self.assertEqual( self.simulate_calls(pc, [-2, -1, -3, -2, 0]), [1010, 1020, 1030, 1040, 1050] ) # For big jumps, we should perhaps alter the schedule, but we # don't currently. This trace shows that we run callbacks # every 10s of time.time(), but the first and second calls are # 110s of real time apart because the backwards jump is # ignored. self.assertEqual(self.simulate_calls(pc, [-100, 0, 0]), [1010, 1020, 1030])
Example #16
Source File: termio.py From django-gateone with GNU General Public License v3.0 | 6 votes |
def _timeout_checker(self): """ Runs `timeout_check` and if there are no more non-sticky patterns in :attr:`self._patterns`, stops :attr:`scheduler`. """ if not self._checking_patterns: self._checking_patterns = True remaining_patterns = self.timeout_check() if not remaining_patterns: # No reason to keep the PeriodicCallback going logging.debug("Stopping self.scheduler (no remaining patterns)") try: self.scheduler.stop() except AttributeError: # Now this is a neat trick: The way IOLoop works with its # stack_context thingamabob the scheduler doesn't actualy end up # inside the MultiplexPOSIXIOLoop instance inside of this # instance of _timeout_checker() *except* inside the main # thread. It is absolutely wacky but it works and works well :) pass self._checking_patterns = False
Example #17
Source File: termio.py From django-gateone with GNU General Public License v3.0 | 6 votes |
def __init__(self, *args, **kwargs): super(MultiplexPOSIXIOLoop, self).__init__(*args, **kwargs) from tornado import ioloop self.terminating = False self.sent_sigint = False self.shell_command = ['/bin/sh', '-c'] self.use_shell = True # Controls whether or not we wrap with the above self.env = {} self.io_loop = ioloop.IOLoop.current() # Monitors child for activity #self.io_loop.set_blocking_signal_threshold(2, self._blocked_io_handler) #signal.signal(signal.SIGALRM, self._blocked_io_handler) self.reenable_timeout = None interval = 100 # A 0.1 second interval should be fast enough self.scheduler = ioloop.PeriodicCallback(self._timeout_checker,interval) self.exitstatus = None self._checking_patterns = False self.read_timeout = datetime.now() self.capture_limit = -1 # Huge reads by default self.restore_rate = None
Example #18
Source File: ioloop_test.py From teleport with Apache License 2.0 | 6 votes |
def simulate_calls(self, pc, durations): """Simulate a series of calls to the PeriodicCallback. Pass a list of call durations in seconds (negative values work to simulate clock adjustments during the call, or more or less equivalently, between calls). This method returns the times at which each call would be made. """ calls = [] now = 1000 pc._next_timeout = now for d in durations: pc._update_next(now) calls.append(pc._next_timeout) now = pc._next_timeout + d return calls
Example #19
Source File: ioloop_test.py From teleport with Apache License 2.0 | 6 votes |
def test_overrun(self): sleep_durations = [9, 9, 10, 11, 20, 20, 35, 35, 0, 0] expected = [ 1010, 1020, 1030, # first 3 calls on schedule 1050, 1070, # next 2 delayed one cycle 1100, 1130, # next 2 delayed 2 cycles 1170, 1210, # next 2 delayed 3 cycles 1220, 1230, # then back on schedule. ] calls = [] def cb(): calls.append(self.io_loop.time()) if not sleep_durations: self.io_loop.stop() return self.io_loop.sleep(sleep_durations.pop(0)) pc = PeriodicCallback(cb, 10000) pc.start() self.io_loop.start() self.assertEqual(calls, expected)
Example #20
Source File: autoreload.py From teleport with Apache License 2.0 | 6 votes |
def start(check_time=500): """Begins watching source files for changes. .. versionchanged:: 5.0 The ``io_loop`` argument (deprecated since version 4.1) has been removed. """ io_loop = ioloop.IOLoop.current() if io_loop in _io_loops: return _io_loops[io_loop] = True if len(_io_loops) > 1: gen_log.warning("tornado.autoreload started more than once in the same process") modify_times = {} callback = functools.partial(_reload_on_update, modify_times) scheduler = ioloop.PeriodicCallback(callback, check_time) scheduler.start()
Example #21
Source File: stats.py From AstroBox with GNU Affero General Public License v3.0 | 6 votes |
def __init__(self, io_loop): # Sessions self.sess_active = 0 # Avoid circular reference self.sess_transports = dict() # Connections self.conn_active = 0 self.conn_ps = MovingAverage() # Packets self.pack_sent_ps = MovingAverage() self.pack_recv_ps = MovingAverage() self._callback = ioloop.PeriodicCallback(self._update, 1000, io_loop) self._callback.start()
Example #22
Source File: client_tornado.py From PyMicroChat with GNU General Public License v3.0 | 6 votes |
def start(self): wait_sec = 10 while True: try: self.stream = yield TCPClient().connect(self.host, self.port) break except iostream.StreamClosedError: logger.error("connect error and again") yield gen.sleep(wait_sec) wait_sec = (wait_sec if (wait_sec >= 60) else (wait_sec * 2)) self.send_heart_beat() self.heartbeat_callback = PeriodicCallback(self.send_heart_beat, 1000 * HEARTBEAT_TIMEOUT) self.heartbeat_callback.start() # start scheduler self.login() self.stream.read_bytes(16, self.__recv_header)
Example #23
Source File: counter.py From kiel with Apache License 2.0 | 6 votes |
def main(): args = parser.parse_args() loop = ioloop.IOLoop.instance() if args.debug: log.setLevel(logging.DEBUG) c = SingleConsumer(brokers=args.brokers) loop.add_callback(run, c, args) status_callback = ioloop.PeriodicCallback( show_status, args.status_interval * 1000 ) def wind_down(_): status_callback.stop() loop.stop() try: status_callback.start() loop.start() except KeyboardInterrupt: c.close().add_done_callback(wind_down)
Example #24
Source File: async.py From django-gateone with GNU General Public License v3.0 | 6 votes |
def __init__(self, callback, callback_time, io_loop=None): self.callback = callback self.callback_time = callback_time self.io_loop = io_loop or IOLoop.current() if self.io_loop._running: # Use a regular PeriodicCallback self._pc = PC(callback, callback_time, io_loop) else: from threading import Timer # NOTE: PeriodicCallback uses ms while Timer uses seconds def callback_wrapper(): "Runs the callback and restarts the Timer so it will run again" self.callback() self._pc = Timer(callback_time / 1000, callback_wrapper) if self._running: self._pc.start() self._pc = Timer(callback_time / 1000, callback_wrapper) self._running = False
Example #25
Source File: ioloop_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
def test_overrun(self): # If a call runs for too long, we skip entire cycles to get # back on schedule. call_durations = [9, 9, 10, 11, 20, 20, 35, 35, 0, 0, 0] expected = [ 1010, 1020, 1030, # first 3 calls on schedule 1050, 1070, # next 2 delayed one cycle 1100, 1130, # next 2 delayed 2 cycles 1170, 1210, # next 2 delayed 3 cycles 1220, 1230, # then back on schedule. ] pc = PeriodicCallback(self.dummy, 10000) self.assertEqual(self.simulate_calls(pc, call_durations), expected)
Example #26
Source File: utils.py From django-gateone with GNU General Public License v3.0 | 5 votes |
def __del__(self): """ Ensures that our `tornado.ioloop.PeriodicCallback` (``self._key_watcher``) gets stopped. """ self._key_watcher.stop()
Example #27
Source File: curl_httpclient.py From honeything with GNU General Public License v3.0 | 5 votes |
def initialize(self, io_loop=None, max_clients=10, max_simultaneous_connections=None): self.io_loop = io_loop self._multi = pycurl.CurlMulti() self._multi.setopt(pycurl.M_TIMERFUNCTION, self._set_timeout) self._multi.setopt(pycurl.M_SOCKETFUNCTION, self._handle_socket) self._curls = [_curl_create(max_simultaneous_connections) for i in xrange(max_clients)] self._free_list = self._curls[:] self._requests = collections.deque() self._fds = {} self._timeout = None try: self._socket_action = self._multi.socket_action except AttributeError: # socket_action is found in pycurl since 7.18.2 (it's been # in libcurl longer than that but wasn't accessible to # python). logging.warning("socket_action method missing from pycurl; " "falling back to socket_all. Upgrading " "libcurl and pycurl will improve performance") self._socket_action = \ lambda fd, action: self._multi.socket_all() # libcurl has bugs that sometimes cause it to not report all # relevant file descriptors and timeouts to TIMERFUNCTION/ # SOCKETFUNCTION. Mitigate the effects of such bugs by # forcing a periodic scan of all active requests. self._force_timeout_callback = ioloop.PeriodicCallback( self._handle_force_timeout, 1000, io_loop=io_loop) self._force_timeout_callback.start()
Example #28
Source File: ioloop_test.py From pySINDy with MIT License | 5 votes |
def test_basic(self): pc = PeriodicCallback(None, 10000) self.assertEqual(self.simulate_calls(pc, [0] * 5), [1010, 1020, 1030, 1040, 1050])
Example #29
Source File: ioloop_test.py From pySINDy with MIT License | 5 votes |
def test_overrun(self): # If a call runs for too long, we skip entire cycles to get # back on schedule. call_durations = [9, 9, 10, 11, 20, 20, 35, 35, 0, 0, 0] expected = [ 1010, 1020, 1030, # first 3 calls on schedule 1050, 1070, # next 2 delayed one cycle 1100, 1130, # next 2 delayed 2 cycles 1170, 1210, # next 2 delayed 3 cycles 1220, 1230, # then back on schedule. ] pc = PeriodicCallback(None, 10000) self.assertEqual(self.simulate_calls(pc, call_durations), expected)
Example #30
Source File: async.py From django-gateone with GNU General Public License v3.0 | 5 votes |
def stop(self): """Stops the timer.""" self._running = False if isinstance(self._pc, PC): # PeriodicCallback() self._pc.stop() else: # Timer() self._pc.cancel()