Search code examples
rubytwitterconcurrencymri

Making parallel calls with twitter gem


I am using twitter gem to make API calls from my app and fetch some data.

I have an array of user_ids and I want to fetch all tweets of each user by doing something like this:

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

Is there any way to make these calls concurrent? Is there any way that I can use typhoeus or any similar gem with twitter? Is there any other way I can make this operation fast?


Solution

  • Wrap your API calls in Ruby threads to run them concurrently:

    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