Skip to content

Instantly share code, notes, and snippets.

@sreeix
Created April 20, 2011 11:44
Show Gist options
  • Save sreeix/931074 to your computer and use it in GitHub Desktop.
Save sreeix/931074 to your computer and use it in GitHub Desktop.
Simple em stuff
require 'fiber'
class SimpleEm
def self.async_fetch(url)
f = Fiber.current
http = EventMachine::HttpRequest.new(url).get :timeout => 10
http.callback { f.resume(http) }
http.errback { f.resume(http) }
return Fiber.yield
end
def self.get_all_sites(sites)
EM.run do
Fiber.new do
sites.each do |site|
print '.'
data = async_fetch("http://#{site}/")
print 'x'
end
EM.stop
end.resume
end
end
def self.get_and_save_sites(sites)
EM.run do
Fiber.new do
sites.each do |site|
print '.'
http = async_fetch("http://#{site}/")
save_file_proc = proc {File.open("data/simple_em#{site}","w+"){|f| f.write(http.response.to_s)}; "data/simple_em#{site}" }
callback = proc {|file_name|puts file_name}
EM.defer save_file_proc, callback
print 'x'
end
EM.stop
end.resume
end
end
def self.get_and_sync_save_sites(sites)
EM.run do
Fiber.new do
sites.each do |site|
print '.'
http = async_fetch("http://#{site}/")
File.open("data/simple_em_sync_save#{site}","w+"){|f| f.write(http.response.to_s)}
print 'x'
end
EM.stop
end.resume
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment