Flexirest: Parallel requests

February 19, 2022 ยท View on GitHub

Sometimes you know you will need to make a bunch of requests and you don't want to wait for one to finish to start the next. When using parallel requests there is the potential to finish many requests all at the same time taking only as long as the single longest request. To use parallel requests you will need to set Flexirest to use a Faraday adapter that supports parallel requests (such as Typhoeus).

# Set adapter to Typhoeus to use parallel requests
Flexirest::Base.adapter = :typhoeus

Now you just need to get ahold of the connection that is going to make the requests by specifying the same host that the models will be using. When inside the in_parallel block call request methods as usual and access the results after the in_parallel block ends.

Flexirest::ConnectionManager.in_parallel('https://www.example.com') do
    @person = Person.find(1234)
    @employers = Employer.all

    puts @person #=> nil
    puts @employers #=> nil
end # The requests are all fired in parallel during this end statement

puts @person.name #=> "John"
puts @employers.size #=> 7

< Empty body handling | Faking calls >