简体   繁体   English

twython搜索api速率限制:标头信息将不会更新

[英]twython search api rate limit: Header information will not be updated

I want to handle the Search-API rate limit of 180 requests / 15 minutes. 我想处理180个请求/ 15分钟的Search-API速率限制。 The first solution I came up with was to check the remaining requests in the header and wait 900 seconds. 我想到的第一个解决方案是检查标头中的其余请求,然后等待900秒。 See the following snippet: 请参见以下代码段:

results = search_interface.cursor(search_interface.search, q=k, lang=lang, result_type=result_mode)

while True:
    try:
        tweet = next(results)
        if limit_reached(search_interface):
            sleep(900)

        self.writer(tweet)


def limit_reached(search_interface):
    remaining_rate = int(search_interface.get_lastfunction_header('X-Rate-Limit-Remaining'))
    return remaining_rate <= 2

But it seems, that the header information are not reseted to 180 after it reached the two remaining requests. 但是,似乎报头信息在到达其余两个请求后并未重置为180。

The second solution I came up with was to handle the twython exception for rate limitation and wait the remaining amount of time: 我想到的第二个解决方案是处理twython异常以进行速率限制并等待剩余时间:

results = search_interface.cursor(search_interface.search, q=k, lang=lang, result_type=result_mode)
while True:
    try:
        tweet = next(results)

        self.writer(tweet)
    except TwythonError as inst:
        logger.error(inst.msg)
        wait_for_reset(search_interface)
        continue
    except StopIteration:
        break


def wait_for_reset(search_interface):
      reset_timestamp = int(search_interface.get_lastfunction_header('X-Rate-Limit-Reset'))
      now_timestamp = datetime.now().timestamp()
      seconds_offset = 10

      t = reset_timestamp - now_timestamp + seconds_offset
      logger.info('Waiting {0} seconds for Twitter rate limit reset.'.format(t))
      sleep(t)

But with this solution I receive this message INFO: Resetting dropped connection: api.twitter.com" and the loop will not continue with the last element of the generator. Have somebody faced the same problems? 但是通过这种解决方案,我收到此信息INFO:重置断开的连接:api.twitter.com”,并且该循环将不会继续使用生成器的最后一个元素。有人遇到过同样的问题吗?

Regards. 问候。

just rate limit yourself is my suggestion (assuming you are constantly hitting the limit ...) 只是限制自己是我的建议(假设您不断达到限制...)

QUERY_PER_SEC = 15*60/180.0  #180 per 15 minutes
#~5 seconds per query
class TwitterBot:
    last_update=0
    def doQuery(self,*args,**kwargs):
        tdiff = time.time()-self.last_update
        if tdiff < QUERY_PER_SEC:
            time.sleep(QUERY_PER_SEC-tdiff) 
        self.last_update = time.time()
        return search_interface.cursor(*args,**kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM