Last active
January 27, 2017 03:40
-
-
Save jwhiteman/c80f959d0c69f3a05b16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'redis' | |
require 'json' | |
require 'timeout' | |
$publisher = Redis.new | |
$subscriber = Redis.new | |
CHANNEL = 'omg' | |
URLS = %w(https://google.com http://yahoo.com https://www.mobilecommons.com) | |
start = Time.now | |
pids = URLS.map do |url| | |
fork do | |
result = HTTParty.get(url) | |
sleep 3 # pretend the result was hard to get | |
data = result.headers.to_a.to_json | |
$publisher.publish CHANNEL, data.to_json | |
end | |
end | |
results = [] | |
begin | |
Timeout.timeout(5) do | |
$subscriber.subscribe(CHANNEL) do |on| | |
on.message do |channel, msg| | |
data = JSON.load(JSON.load(msg)) # hack | |
results << data | |
end | |
end | |
end | |
rescue | |
done = Time.now - start | |
puts "Done in #{done}" | |
puts results.inspect | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
URLS = %w(https://google.com http://yahoo.com https://www.mobilecommons.com) | |
results = [] | |
start = Time.now | |
URLS.map do |url| | |
Thread.new(results) do |results| | |
results << HTTParty.get(url).response | |
sleep 3 | |
end | |
end.each &:join | |
done = Time.now - start | |
puts results.inspect | |
puts "Done in #{done}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment