Sha256: 94d38a698052142ca5fd45c111f79d24fd7f34c2b2c1fb8ea3aa79da2a2afa45

Contents?: true

Size: 987 Bytes

Versions: 32

Compression:

Stored size: 987 Bytes

Contents

#!/usr/bin/env ruby
#
# Example of using the HTTP Gem with Celluloid::IO
# Make sure to 'gem install celluloid-io' before running
#
# Run as: bundle exec examples/parallel_requests_with_celluloid.rb
#

require "celluloid/io"
require "http"

class HttpFetcher
  include Celluloid::IO

  def fetch(url)
    # Note: For SSL support specify:
    # ssl_socket_class: Celluloid::IO::SSLSocket
    HTTP.get(url, :socket_class => Celluloid::IO::TCPSocket)
  end
end

fetcher = HttpFetcher.new

urls = %w(http://ruby-lang.org/ http://rubygems.org/ http://celluloid.io/)

# Kick off a bunch of future calls to HttpFetcher to grab the URLs in parallel
futures = urls.map { |u| [u, fetcher.future.fetch(u)] }

# Consume the results as they come in
futures.each do |url, future|
  # Wait for HttpFetcher#fetch to complete for this request
  response = future.value
  puts "Got #{url}: #{response.inspect}"
end

# Suppress Celluloid's shutdown messages
# Otherwise the example is a bit noisy :|
exit!

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
http-0.9.9 examples/parallel_requests_with_celluloid.rb
http-1.0.0.pre1 examples/parallel_requests_with_celluloid.rb
http-0.9.8 examples/parallel_requests_with_celluloid.rb
http-0.9.7 examples/parallel_requests_with_celluloid.rb
http-0.9.6 examples/parallel_requests_with_celluloid.rb
http-0.9.5 examples/parallel_requests_with_celluloid.rb
http-0.9.4 examples/parallel_requests_with_celluloid.rb
http-0.9.3 examples/parallel_requests_with_celluloid.rb
http-0.8.14 examples/parallel_requests_with_celluloid.rb
http-0.9.2 examples/parallel_requests_with_celluloid.rb
http-0.8.13 examples/parallel_requests_with_celluloid.rb
http-0.9.1 examples/parallel_requests_with_celluloid.rb
http-0.9.0 examples/parallel_requests_with_celluloid.rb
http-0.9.0.pre examples/parallel_requests_with_celluloid.rb
http-0.8.12 examples/parallel_requests_with_celluloid.rb
http-0.8.11 examples/parallel_requests_with_celluloid.rb
http-0.8.10 examples/parallel_requests_with_celluloid.rb
http-0.8.9 examples/parallel_requests_with_celluloid.rb
http-0.8.8 examples/parallel_requests_with_celluloid.rb
http-0.8.7 examples/parallel_requests_with_celluloid.rb