使用 Twitter 进行并行调用 gem

Making parallel calls with twitter gem

我正在使用 twitter gem 从我的应用进行 API 调用并获取一些数据。

我有一个 user_ids 数组,我想通过执行以下操作来获取每个用户的所有推文:

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

有没有办法让这些调用并发?有什么方法可以让 typhoeus 或任何类似的 gem 与 twitter 一起使用?有没有其他方法可以让这个操作更快?

将你的 API 调用包装在 Ruby threads 到 运行 并发:

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