执行过期使用`sleep`进行Twitter gem查询

时间:2013-05-27 21:25:43

标签: ruby twitter sleep

我正在调用Twitter的GET friends/idsGET users/lookup资源。它们工作得很好,直到我考虑到速率限制。以下是工作代码的示例:

def followers_ids(screen_name)
  cursor = "-1"
  followers_ids = []
  while cursor != 0 do
    followers = Twitter.follower_ids(screen_name, :cursor => cursor)

    cursor = followers.next_cursor
    followers_ids << followers.ids
  end
  followers_ids.flatten!
  followers_ids
end

当我将sleep扔进while循环时,如下所示:

while cursor != 0 do
  followers = Twitter.follower_ids(screen_name, :cursor => cursor)

  cursor = followers.next_cursor
  followers_ids << followers.ids
  sleep(60)
end

我在不同的时间得到不同的错误集。

C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:799in 'connect': execution expired (Twitter::Error::ClientError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:799:in 'block in connect'
..
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/adapter/net_http.rb:73:in 'perform_request'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/adapter/net_http.rb:38:in 'call'

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/twitter-4.6.2/lib/twitter/response/raise_error.rb:21:in 'on_complete': Twitter is down or being upgraded (Twitter::Error:BadGateway)
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/faraday-0.8.7/lib/faraday/response.rb:9:in 'block in call'
..

任何人都知道会发生什么事吗?我曾在某处读到过,当你睡觉时,你的程序会忽略所有的消息。这可能是它的一部分吗?我有更好的方法吗?

0 个答案:

没有答案