Skip to content

Instantly share code, notes, and snippets.

@universal
Created October 12, 2012 07:27
Show Gist options
  • Save universal/3877787 to your computer and use it in GitHub Desktop.
Save universal/3877787 to your computer and use it in GitHub Desktop.
ruby parallelism
irb(main):003:0> fs.fetch_serial
Google
Google
SPIEGEL ONLINE - Nachrichten
IT-News, c't, iX, Technology Review, Telepolis | heise online
Golem.de: IT-News für Profis
Planet RubyOnRails | Working with a wealth of web servers and databases
Ruby on Rails
took: 12.69326297 seconds
=> nil
irb(main):004:0> fs.fetch_parallel
Google
Google
Golem.de: IT-News für Profis
SPIEGEL ONLINE - Nachrichten
Ruby on Rails
IT-News, c't, iX, Technology Review, Telepolis | heise online
Planet RubyOnRails | Working with a wealth of web servers and databases
took: 2.317157908 seconds
class FetchSites
attr_reader :sites
def initialize
@sites = ["google.com", "google.de", "spiegel.de", "heise.de", "golem.de", "planetrubyonrails.org", "rubyonrails.org"]
end
def fetch_serial
start_time = Time.now
sites.each do |site|
agent = Mechanize.new
page = agent.get("http://#{site}")
puts page.title
end
end_time = Time.now
puts "took: #{end_time - start_time} seconds"
end
def fetch_parallel
start_time = Time.now
threads = []
sites.each do |site|
threads << Thread.new do
agent = Mechanize.new
page = agent.get("http://#{site}")
puts page.title
end
end
threads.each do |thread|
thread.join
end
end_time = Time.now
puts "took: #{end_time - start_time} seconds"
end
end
source 'https://rubygems.org'
gem 'rails', '3.2.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'ember-rails'
gem 'therubyracer'
gem 'mechanize'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment