import requests
import time


def get_results_with_retry(wdt_sparql_url, query):
    """
    Run SPARQL query multiple times until the results are there.
    """
    while True:
        try:
            r = requests.get(wdt_sparql_url,
                     params = {'format': 'json', 'query': query})
        #    res_text=r.text
        #    response = json.loads(res_text)
            response = r.json()
            break
        except Exception as e:
            print(e, 'error, retrying')
            time.sleep(2)
            continue
    return response