'Run query and check status until it finishes or timeouts.
Check status until it finishes or timeouts.'

def execute_and_wait(self, query, timeout_sec=30.0, sleep_interval=0.5):

    handle = self.client.query(query)
    curr = time.time()
    end = (curr + timeout_sec)
    while (curr <= end):
        state = self.client.get_state(handle)
        if (state not in (QueryHistory.STATE.running, QueryHistory.STATE.submitted)):
            return handle
        time.sleep(sleep_interval)
        curr = time.time()
    msg = ('The    query    timed    out    after    %(timeout)d    seconds,    canceled    query.' % {'timeout': timeout_sec})
    LOG.warning(msg)
    try:
        self.cancel_operation(handle)
    except Exception as e:
        msg = 'Failed    to    cancel    query.'
        LOG.warning(msg)
        self.close_operation(handle)
        raise QueryServerException(e, message=msg)
    raise QueryServerTimeoutException(message=msg)
