def update_in_ioloop(update):
    """Decorator that ensures an update() method is run in the tornado ioloop.

    Does this by checking the thread identity. Requires that the object to
    which the method is bound has the attributes :attr:`_ioloop_thread_id`
    (the result of thread.get_ident() in the ioloop thread) and :attr:`ioloop`
    (the ioloop instance in use). Also assumes the signature
    `update(self, sensor, reading)` for the method.


    """
    @wraps(update)
    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)

    return wrapped_update