如何加快API请求? [英] How to speed up API requests?

查看:159
本文介绍了如何加快API请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了使用谷歌的地方API获取电话号码以下的小程序,但它是pretty缓慢。当我有6个项目的测试需要从任何地方4.86s到1.99s,我不知道为什么在时间上显著的变化。我很新的API,所以我甚至不知道什么样的东西能/不能加快,这样的东西都留给了Web服务器的服务API和什么我可以改变我自己。

 导入请求,JSON,时间
sea​​rchTerms =输入(用逗号隔开输入的地方)START_TIME =选定了time.time()#timer
sea​​rchTerms = searchTerms.split(,)
因为我在searchTerms:
    R1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query='+我+'和;键= MY_KEY')
    A = r1.json()
    PID =一['结果'] [0] ['place_id']
    R2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+pid+'&key=MY_KEY')
    B = r2.json()
    手机= B ['结果'] ['FORMATTED_PHONE_NUMBER']
    名称= B ['结果'] ['名']
    网站= B ['结果'] ['网站']
    打印(电话+''+名字+''+网站)打印(---%S秒---%(选定了time.time() - START_TIME))


解决方案

您可能希望并行发送请求。 Python提供 多处理 模块它适合于这样的任务。

样code:

 从多进口游泳池高清GET_DATA(ⅰ):
    R1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query='+我+'和;键= MY_KEY')
    A = r1.json()
    PID =一['结果'] [0] ['place_id']
    R2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+pid+'&key=MY_KEY')
    B = r2.json()
    手机= B ['结果'] ['FORMATTED_PHONE_NUMBER']
    名称= B ['结果'] ['名']
    网站= B ['结果'] ['网站']
    回报'。加入((电话,名称,网站))如果__name__ =='__main__':
    术语=输入(输入的地方用逗号分隔)。分裂()
    配有游泳池(5)为p:
        打印(p.map(GET_DATA,条款))

I've constructed the following little program for getting phone numbers using google's place api but it's pretty slow. When I'm testing with 6 items it takes anywhere from 4.86s to 1.99s and I'm not sure why the significant change in time. I'm very new to API's so I'm not even sure what sort of things can/cannot be sped up, which sort of things are left to the webserver servicing the API and what I can change myself.

import requests,json,time
searchTerms = input("input places separated by comma")

start_time = time.time() #timer
searchTerms = searchTerms.split(',')
for i in searchTerms:
    r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query='+ i +'&key=MY_KEY')
    a = r1.json()
    pid = a['results'][0]['place_id']
    r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+pid+'&key=MY_KEY')
    b = r2.json()
    phone = b['result']['formatted_phone_number']
    name = b['result']['name']
    website = b['result']['website']
    print(phone+' '+name+' '+website)

print("--- %s seconds ---" % (time.time() - start_time))

解决方案

You may want to send requests in parallel. Python provides multiprocessing module which is suitable for task like this.

Sample code:

from multiprocessing import Pool

def get_data(i):
    r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query='+ i +'&key=MY_KEY')
    a = r1.json()
    pid = a['results'][0]['place_id']
    r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid='+pid+'&key=MY_KEY')
    b = r2.json()
    phone = b['result']['formatted_phone_number']
    name = b['result']['name']
    website = b['result']['website']
    return ' '.join((phone, name, website))

if __name__ == '__main__':
    terms = input("input places separated by comma").split(",")
    with Pool(5) as p:
        print(p.map(get_data, terms))

这篇关于如何加快API请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆