Sha256: 97e275235334927dc9a305cbfc14eeac4b3d266c79b8a1851878ee25c9bee953

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", __dir__)
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

2 entries across 2 versions & 1 rubygems

Version Path
celluloid-0.18.0 examples/futures.rb
celluloid-0.18.0.pre2 examples/futures.rb