Created
October 8, 2014 16:10
-
-
Save monolar/75deb6db2e4d084d0a71 to your computer and use it in GitHub Desktop.
Trying to understand pools
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'celluloid' | |
require 'chromatic' | |
$stdout.sync = true | |
Celluloid.logger = nil | |
WITH_POOL = true | |
class Executor | |
include Celluloid | |
def initialize | |
puts self.inspect.magenta | |
end | |
def run(i) | |
print " +#{i}+ ".light_green | |
sleep 1 | |
print " -#{i}- ".light_red | |
end | |
end | |
# in this case only two executors run in parallel | |
if WITH_POOL | |
supervision_group = Celluloid::SupervisionGroup.run! | |
supervision_group.pool(Executor, { | |
as: :executor, | |
size: 2 | |
}) | |
else | |
# in this case all ten executors run in parallel | |
Celluloid::Actor[:executor] = Executor.new | |
end | |
start = Time.now | |
(1..10).map { |i| | |
Celluloid::Actor[:executor].future.run(i) | |
}.map(&:value) | |
finish = Time.now | |
puts "\ndone (%.2fs)".green % (finish - start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output with pool:
Output without pool: