Skip to content

Commit

Permalink
Merge e7e4532 into 6d72331
Browse files Browse the repository at this point in the history
  • Loading branch information
p-vinayak committed Aug 28, 2020
2 parents 6d72331 + e7e4532 commit 7c480c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
32 changes: 32 additions & 0 deletions examples/pool.rb
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby

$LOAD_PATH.push File.expand_path("../lib", __dir__)
require "celluloid/autostart"

# This is required for pool to work as Celluloid doesn't load it by default
require "celluloid/pool"
require "prime"

class PrimeWorker
include Celluloid

# Checks if a number is prime
def prime(number)
if Prime.prime?(number)
puts number
end
end
end

# Creates a pool of actors that is equal to the number of CPU cores present on
# the machine
pool = PrimeWorker.pool

(2..1000).to_a.map do |i|
# Asynchronously calls prime method. Celluloid decides which actor to invoke
# out of the pool
pool.async.prime i
end

# Keeps the main thread alive for long enough for our output to be displayed
sleep 100
8 changes: 6 additions & 2 deletions lib/celluloid.rb
Expand Up @@ -245,6 +245,10 @@ def behavior_options
def ===(other)
other.is_a? self
end

def pool
raise NoMethodError, "Missing require for \"celluloid/pool\". Add 'require \"celluloid/pool\" for expected behavior."
end
end

# These are methods we don't want added to the Celluloid singleton but to be
Expand Down Expand Up @@ -488,11 +492,11 @@ def future(meth = nil, *args, &block)

require "celluloid/group"
require "celluloid/group/spawner"
require "celluloid/group/pool" # TODO: Find way to only load this if being used.
require "celluloid/group/pool"

require "celluloid/task"
require "celluloid/task/fibered"
require "celluloid/task/threaded" # TODO: Find way to only load this if being used.
require "celluloid/task/threaded" # TODO: Find way to only load this if being used.

require "celluloid/actor"
require "celluloid/cell"
Expand Down

0 comments on commit 7c480c8

Please sign in to comment.