Skip to content

Commit

Permalink
Merge pull request #404 from mbrookes/patch-2
Browse files Browse the repository at this point in the history
Update Readme.md parallel requests example.
  • Loading branch information
hanshasselberg committed Sep 16, 2014
2 parents 6867604 + 0062fb2 commit 84aeca2
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,25 @@ request.run

### Making Parallel Requests

Generally, you should be running requests through hydra. Here is how that looks
Generally, you should be running requests through hydra. Here is how that looks:

```ruby
hydra = Typhoeus::Hydra.hydra

first_request = Typhoeus::Request.new("www.example.com/posts/1.json")
first_request = Typhoeus::Request.new("http://example.com/posts/1")
first_request.on_complete do |response|
third_request = Typhoeus::Request.new("www.example.com/posts/3.json")
third_url = response.body
third_request = Typhoeus::Request.new(third_url)
hydra.queue third_request
end
second_request = Typhoeus::Request.new("www.example.com/posts/2.json")
second_request = Typhoeus::Request.new("http://example.com/posts/2")

hydra.queue first_request
hydra.queue second_request
# this is a blocking call that returns once all requests are complete
hydra.run
hydra.run # this is a blocking call that returns once all requests are complete
```

The execution of that code goes something like this. The first and second requests are built and queued. When hydra is run the first and second requests run in parallel. When the first request completes, the third request is then built and queued up. The moment it is queued Hydra starts executing it. Meanwhile the second request would continue to run (or it could have completed before the first). Once the third request is done, `hydra.run` returns.
The execution of that code goes something like this. The first and second requests are built and queued. When hydra is run the first and second requests run in parallel. When the first request completes, the third request is then built and queued, in this example based on the result of the first request. The moment it is queued Hydra starts executing it. Meanwhile the second request would continue to run (or it could have completed before the first). Once the third request is done, `hydra.run` returns.

How to get an array of response bodies back after executing a queue:

Expand Down

0 comments on commit 84aeca2

Please sign in to comment.