0

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

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

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

有没有办法让这些调用并发?有什么方法可以让我在twitter上使用typhoeus或任何类似的 gem吗?有没有其他方法可以快速完成此操作?

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 回答