有没有更快的方法从REST API请求数据?

时间:2019-01-15 13:04:58

标签: python-3.x pandas performance rest

我正在尝试加快以下代码的速度。目前大约需要15分钟才能运行。...

我正在访问的数据源每页仅返回1000行,行数未知,因此我不停地迭代思想页以获取完整的数据集。

我不确定为什么这么慢,数据源正在本地服务器上运行。

任何帮助都会很棒!

def calcreadings(calc_elements_id,unit_of_time,length_of_data,size = 1000,page = 1):     尝试:     #导入所需的库         汇入要求         将熊猫作为pd导入

    canary_server, access_token = config()
    calc_elements_id = str(calc_elements_id)
    length_of_data = str(length_of_data)
    unit_of_time = str(unit_of_time)
    # sets up the requeste to collect data from the API

    pages_check = page

    # loops over the pages in the api

    while pages_check > 0:
        request = requests.get(canary_server
                            + 'calcelements/' + calc_elements_id
                            + '/data/?'
                            + 'periodType=' + unit_of_time
                            + '&active=false'
                            + '&_size=' + str(size)
                            + '&_page=' + str(page)
                            + '&_sort= Id DESC'
                            + '&periodLength=' + str(length_of_data)
                            + '&access_token=' + access_token)

        calc_readings_check = pd.read_json(request.text)
        if len(calc_readings_check) != 0:
            if page == 1:
                # loads the data from the API in to Pandas Data Frame
                calc_readings = calc_readings_check
            else:
                # loads the data from the API in to Pandas Data Frame and appends
                calc_readings = calc_readings.append(calc_readings_check)
            page += 1
        else:
            pages_check = 0
    try:
        calc_readings['calc_elements_id'] = int(calc_elements_id)
    except NameError:
        calc_readings = ''
    return calc_readings
except:
    pass

0 个答案:

没有答案