Sha256: a0a81b28a83a3ed371b7cb1df7bee3731ff7543dd0b7b2bfdf2908f3530b2e49

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
require "celluloid/autostart"
require "digest/sha2"

class Hasher
  include Celluloid

  def initialize(secret)
    @hash = Digest::SHA2.hexdigest(secret)
  end

  # Add some data into our hash. This demonstrates a non-trivial computation
  # of the same sort as, say, calculating Fibonacci numbers. Since Celluloid
  # uses several threads, doing something like this won't grind our entire
  # application to a halt
  def add(data, n = 100_000)
    string = @hash + data
    n.times { string = Digest::SHA2.hexdigest(string) }
    @hash = string
  end
end

# Create the hasher
hasher = Hasher.new("super secret initialization data")

# Ask the hasher to perform a complex computation. However, since we're using
# a future, this doesn't block the current thread
future = hasher.future.add("some data to be hashed")

# We've kicked off the hasher, but this thread can continue performing other
# activities while the hasher runs in the background
puts "The hasher is now running, but this thread is free to do whatever it wants"

# Now let's ask for the return value from the hasher
puts "Getting the hasher's return value... "
p future.value

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
celluloid-0.17.4 examples/futures.rb
celluloid-0.18.0.pre examples/futures.rb
celluloid-0.17.3 examples/futures.rb
celluloid-0.17.2 examples/futures.rb
celluloid-0.17.1.2 examples/futures.rb
celluloid-0.17.1.1 examples/futures.rb
celluloid-0.17.1 examples/futures.rb
celluloid-0.17.0 examples/futures.rb