예제 #1
0
 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)
예제 #2
0
 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'
예제 #3
0
    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')
예제 #4
0
 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')))
예제 #5
0
 def _install(self):
     self.ioloop_thread_id = get_thread_ident()
     self._running.set()
     yield self._connect()
예제 #6
0
 def _install(self):
     self._thread_id = get_thread_ident()
예제 #7
0
 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)
예제 #8
0
 def first_run():
     self._ioloop_thread_id = get_thread_ident()
     if self.OBSERVE_UPDATES:
         self.attach()
예제 #9
0
 def only_in_ioloop(self):
     test_inst.assertEqual(get_thread_ident(), self.thread_id)
     return 'only_in'
예제 #10
0
 def not_in_ioloop(self):
     test_inst.assertNotEqual(get_thread_ident(), self.thread_id)
     return 'not_in'
예제 #11
0
 def a_callable(self, arg, kwarg='abc'):
     test_inst.assertEqual(get_thread_ident(), self.thread_id)
     return (arg * 2, kwarg * 3)