Class: Parapool
- Inherits:
-
Object
- Object
- Parapool
- Defined in:
- lib/parapool.rb,
lib/parapool/job.rb,
lib/parapool/error.rb,
lib/parapool/worker.rb,
lib/parapool/version.rb,
lib/parapool/synchronizer.rb
Defined Under Namespace
Classes: Error, Job, Synchronizer, Worker
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
Instance Method Summary collapse
-
#initialize(pool_size = 4) ⇒ Parapool
constructor
A new instance of Parapool.
- #map(params, &block) ⇒ Object
- #release ⇒ Object
Constructor Details
#initialize(pool_size = 4) ⇒ Parapool
Returns a new instance of Parapool.
11 12 13 14 15 16 17 18 19 |
# File 'lib/parapool.rb', line 11 def initialize(pool_size = 4) raise ArgumentError, 'Pool size must be greater than or equal to 1.' if pool_size < 1 @pool_size = pool_size @queue = Queue.new @workers = [] create_worker end |
Instance Attribute Details
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
9 10 11 |
# File 'lib/parapool.rb', line 9 def pool_size @pool_size end |
Instance Method Details
#map(params, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/parapool.rb', line 21 def map(params, &block) raise TypeError, "wrong argument type #{params.class} (expected Enumerable)" unless params.is_a?(Enumerable) raise Parapool::Error, 'must be called with a block' unless block_given? return if params.empty? sync = Synchronizer.new(params.size) jobs = [] params.each do |param| job = Job.new(param, sync, &block) push(job) jobs << job end sync.wait jobs.map { |job| job.result } end |
#release ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/parapool.rb', line 41 def release @workers.size.times.each do @queue.push(nil) end @workers.each do |worker| worker.join end end |