Python递归无法运行一千次

我编写了一个函数来使用异步请求执行API调用,因为我正在处理上千个请求,并且需要控制速率限制。

如果我有一个要运行3000或4000个对象的列表,我的函数就可以工作,但是如果它是一个更大的列表(如40000),则通常会崩溃-通常我会遇到以下异常:

超过最大递归深度

由于异步请求允许发送一组同时运行的请求,因此我的函数以递归方式运行,直到请求长度等于零为止。

如果Python不适合使用递归,那么我的其他选择是什么?

def __get_tickets_audits(conn_id, remaining_requests, req_size=50, result=[], sleep=15):
    def append_response(response):
        _body = response.get("_body")
        if _body.get("error", None) is not None:
            print(_body)
        else:
            audits = _body.get("audits", [])
            ticket_id = audits[0]["ticket_id"]
            result.append({"id": ticket_id, "data": audits})

    log.info(f"Remaining requests {len(remaining)}")
    if len(remaining_requests) > 0:
        hook = AsyncHttpRequestsHook(conn_id)
        first_response = asyncio.get_event_loop().run_until_complete(hook.calls([remaining_requests.pop(0)]))[0]
        x_rate_limit_remaining = first_response["headers"]["X-Rate-Limit-Remaining"]
        log.info(f"X-Rate-Remaining: {x_rate_limit_remaining}")
        append_response(first_response)
        if int(x_rate_limit_remaining) >= (MIN_RATE_REMAINING + req_size):
            max_range = req_size if len(remaining_requests) >= req_size else len(remaining_requests)
            subsequent = [remaining_requests.pop() for _ in range(0, max_range)]
            log.info(f"Audits to retrieve: {len(subsequent)}")
            subsequent_response = asyncio.get_event_loop().run_until_complete(hook.calls(subsequent))
            log.info(f"Audits retrieved: {len(subsequent_response)}")
            [append_response(x) for x in subsequent_response]
        else:
            log.info(f"Need for Sleeping for {sleep} seconds from now")
            time.sleep(sleep)
        return __get_tickets_audits(conn_id, remaining_requests, result=result)
    return result

Maybe switch for a while len(remaining_requests) > 0?

评论