【问题标题】:How to throttle Grequests when making asynchronous API calls?进行异步 API 调用时如何限制 Grequests?
【发布时间】:2019-01-31 00:28:23
【问题描述】:

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

API 限制为每秒 100 次调用。

如何限制 grequest 以每秒仅传递 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)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多