Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters