python - 进行异步 API 调用时如何限制 Grequests?

标签 python python-3.x asynchronous api-design sendasynchronousrequest

我正在使用 grequests 库传递约 250000 个 url 以从 api 获取数据。

API 的调用限制为每秒 100 次。

如何将请求限制为每秒仅传递 100 个 URL?我将大小参数从 5 增加到 100。不确定这会做什么,但仍然会出现错误“超出最大重试次数”。

这是迄今为止我的代码:

import grequests

lst = ['url.com','url2.com']

class Test:
    def __init__(self):
        self.urls = lst

    def exception(self, request, exception):
        print ("Problem: {}: {}".format(request.url, exception))

    def async(self):
        return grequests.map((grequests.get(u) for u in self.urls), exception_handler=self.exception, size=100)

    def collate_responses(self, results):
        return [x.text for x in results]

test = Test()
#here we collect the results returned by the async function
results = test.async()

response_text = test.collate_responses(results)

最佳答案

Grequests 似乎发出了 100 个请求,然后在没有任何等待的情况下发出了另外 100 个请求,依此类推。这些请求之间没有定义时间。 这是一个类似的问题及其解决方案: Limiting/throttling the rate of HTTP requests in GRequests

关于python - 进行异步 API 调用时如何限制 Grequests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005901/

相关文章:

python - 如何使用 solve_ivp 通过精确点?

python - 如何使用 float64 nan 选择行?

python - 过滤和计算 Pandas 行的更快方法

python - 如何使用 Python 将多个标签变量转换为 tkinter 中的字典?

python - 在图像上滑动窗口 OpenCV

Python:在 setter 中使用 getter 或 "private"属性?

javascript - Pyodide languagePluginLoader 未定义

.net - 如何异步运行 NHibenate 查询?

javascript - 如何在 Rxjs 中对不完整序列进行 Observable 计数

javascript - 如何从异步调用返回响应?