Skip to content

Instantly share code, notes, and snippets.

@jwhiteman
Last active January 27, 2017 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwhiteman/c80f959d0c69f3a05b16 to your computer and use it in GitHub Desktop.
Save jwhiteman/c80f959d0c69f3a05b16 to your computer and use it in GitHub Desktop.
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