File: futures.rb

package info (click to toggle)
ruby-celluloid 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: ruby: 7,579; makefile: 10
file content (38 lines) | stat: -rwxr-xr-x 1,216 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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