import time


def get_return_value(job, poll_interval=0.05, timeout=30):
    """
    Poll the job until it returns a non-None value or an Exception, or
    the polling times out.

    Parameters
    ----------
    job: DelayedJob
    poll_interval: float [.05]
        In seconds.
    timeout: float [30]
        In seconds.
    """
    t = time.time()
    while job.return_value is None and (time.time() - t) < timeout:
        time.sleep(poll_interval)
    if isinstance(job.return_value, Exception):
        raise job.return_value
    print("get_return_value: returning after {:.3f} s".format(
        time.time() - t))
    return job.return_value