def _run_once(self): assert get_thread_ident() == self._ioloop_thread_id now = self.ioloop.time() self.inform(self._sensor.read()) self.next_time += self._period if self.next_time < now: # Catch up if we have fallen far behind self.next_time = now + self._period self.next_timeout_handle = self.ioloop.call_at(self.next_time, self._run_once)
def call_in_ioloop(self, fn, args, kwargs, timeout=None): timeout = timeout or self.default_timeout if get_thread_ident() == self._thread_id: raise RuntimeError("Cannot call a thread-wrapped object from the ioloop") future, tornado_future = Future(), tornado_Future() self.ioloop.add_callback( self._ioloop_call, future, tornado_future, fn, args, kwargs) try: # Use the thread-safe future to block return future.result(timeout) except TimeoutError: raise except Exception: # If we have an exception use the tornado future instead since it # will print a nicer traceback. tornado_future.result() # Should never get here since the tornado future should raise assert False, 'Tornado Future should have raised'
def test_wrapping(self): test_inst = self class Wrappee(object): def __init__(self, ioloop_thread_id): self.thread_id = ioloop_thread_id def a_callable(self, arg, kwarg='abc'): test_inst.assertEqual(get_thread_ident(), self.thread_id) return (arg * 2, kwarg * 3) @property def not_in_ioloop(self): test_inst.assertNotEqual(get_thread_ident(), self.thread_id) return 'not_in' @property def only_in_ioloop(self): test_inst.assertEqual(get_thread_ident(), self.thread_id) return 'only_in' class TestWrapper(ioloop_manager.ThreadSafeMethodAttrWrapper): @property def only_in_ioloop(self): return self._getattr('only_in_ioloop') id_future = Future() self.ioloop.add_callback(lambda : id_future.set_result(get_thread_ident())) wrappee = Wrappee(id_future.result(timeout=1)) wrapped = TestWrapper(wrappee, self.ioloop_thread_wrapper) # First test our assumptions about Wrappee with self.assertRaises(AssertionError): wrappee.a_callable(3, 'a') with self.assertRaises(AssertionError): wrappee.only_in_ioloop self.assertEqual(wrappee.not_in_ioloop, 'not_in') # Now test the wrapped version self.assertEqual(wrapped.a_callable(5, kwarg='bcd'), (10, 'bcd'*3)) self.assertEqual(wrapped.only_in_ioloop, 'only_in') self.assertEqual(wrapped.not_in_ioloop, 'not_in')
def _connect(self): assert get_thread_ident() == self.ioloop_thread_id self._stream = AttrDict(error=None) self._connected.set() self.notify_connected(True) self.preset_protocol_flags(ProtocolFlags(5, 0, set('IM')))
def _install(self): self.ioloop_thread_id = get_thread_ident() self._running.set() yield self._connect()
def _install(self): self._thread_id = get_thread_ident()
def wrapped_update(self, sensor, reading): if get_thread_ident() == self._ioloop_thread_id: update(self, sensor, reading) else: self.ioloop.add_callback(update, self, sensor, reading)
def first_run(): self._ioloop_thread_id = get_thread_ident() if self.OBSERVE_UPDATES: self.attach()
def only_in_ioloop(self): test_inst.assertEqual(get_thread_ident(), self.thread_id) return 'only_in'
def not_in_ioloop(self): test_inst.assertNotEqual(get_thread_ident(), self.thread_id) return 'not_in'
def a_callable(self, arg, kwarg='abc'): test_inst.assertEqual(get_thread_ident(), self.thread_id) return (arg * 2, kwarg * 3)