Skip to content

Instantly share code, notes, and snippets.

@monolar
Created October 8, 2014 16:10
Show Gist options
  • Save monolar/75deb6db2e4d084d0a71 to your computer and use it in GitHub Desktop.
Save monolar/75deb6db2e4d084d0a71 to your computer and use it in GitHub Desktop.
Trying to understand pools
#!/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)
@monolar
Copy link
Author

monolar commented Oct 8, 2014

  • Output with pool:

    $ ./pool.rb 
    #
    #
    +1+  +2+  -2-  -1-  +3+  +4+  -4-  -3-  +5+  +6+  -5-  -6-  +7+  +8+  -8-  -7-  +9+  +10+  -10-  -9- 
    done (5.02s)
    
  • Output without pool:

    $ ./pool.rb 
    #
    +1+  +2+  +3+  +4+  +5+  +6+  +7+  +8+  +9+  +10+  -1-  -2-  -3-  -4-  -5-  -6-  -7-  -8-  -9-  -10- 
    done (1.00s)
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment