Skip to content

Instantly share code, notes, and snippets.

@jamesshipton
Last active August 29, 2015 14:14
Show Gist options
  • Save jamesshipton/abe8015394754f9ed9ab to your computer and use it in GitHub Desktop.
Save jamesshipton/abe8015394754f9ed9ab to your computer and use it in GitHub Desktop.
synchronously.rb
# Synchronously
class Fetcher
def self.fetch_api_data(uuids)
uuids.map { |uuid| new(uuid).fetch }
end
def fetch
"api response for #{uuid}"
end
private
attr_accessor :uuid
def initialize(uuid)
self.uuid = uuid
end
end
# Asynchronously
class Fetcher
include Celluloid
def self.fetch_api_data(uuids)
actors = uuids.map { |uuid| new(uuid) }
responses = actors.map(&:future).map(&:fetch).map(&:value)
actors.map(&:terminate)
responses
end
def fetch
"api response for #{uuid}"
end
private
attr_accessor :uuid
def initialize(uuid)
self.uuid = uuid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment