0

Twitter gem を使用して、アプリから API 呼び出しを行い、データを取得しています。

の配列がuser_idsあり、次のようにして各ユーザーのすべてのツイートを取得したいと考えています。

user_ids.map {|user_id| Client.user_timeline(user_id)}

これらの呼び出しを同時に行う方法はありますか? typhoeusまたは同様の gem をtwitterで使用できる方法はありますか? この操作を高速化する他の方法はありますか?

4

1 に答える 1

1

API 呼び出しをRuby スレッドでラップして、同時に実行します。

tweets, threads = [], []

threads = user_ids.map do |user_id|
  Thread.new { tweets << Client.user_timeline(user_id) }
end

threads.each(&:join)

# All the tweets are in the `tweets` array
puts tweets
于 2016-04-23T14:14:52.303 に答える