Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
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
You can’t perform that action at this time.