Python中的异步JSON请求

时间:2015-10-27 18:39:09

标签: python json http asynchronous python-requests

我正在使用API​​来执行返回JSON的HTTP请求。但是,调用api取决于要指示的开始页和结束页,例如:

def API_request(URL):
    while(True):
        try:
            Response = requests.get(URL)
            Data = Response.json()
            return(Data['data'])
        except Exception as APIError:
            print(APIError)
            continue
        break
def build_orglist(start_page, end_page):
    APILink = ("http://sc-api.com/?api_source=live&system=organizations&action="
               "all_organizations&source=rsi&start_page={0}&end_page={1}&items_"
               "per_page=500&sort_method=&sort_direction=ascending&expedite=1&f"
               "ormat=json".format(start_page, end_page))
    return(API_request(APILink))

了解您是否不在现有页面的唯一方法是,当JSON为空时,如this

如果我想在每个页面上异步执行多个build_orglist,直到我到达终点(Null JSON),我该怎么办?

1 个答案:

答案 0 :(得分:0)

我使用了@LukasGraf的回答,使用会话将我的所有HTTP连接统一到一个会话中,并使用grequests并行地建立HTTP请求组