Class: Vuf::WorkingPool
- Inherits:
-
Object
- Object
- Vuf::WorkingPool
- Defined in:
- lib/vuf/working_pool.rb
Constant Summary collapse
- ENDING_TASK =
"ENDING_TASK"
Instance Method Summary collapse
- #do(channel = nil, *args, &task) ⇒ Object
- #finalize ⇒ Object
-
#initialize(nb_workers, max_pending_tasks = nil) ⇒ WorkingPool
constructor
A new instance of WorkingPool.
- #run ⇒ Object
Constructor Details
#initialize(nb_workers, max_pending_tasks = nil) ⇒ WorkingPool
Returns a new instance of WorkingPool.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/vuf/working_pool.rb', line 5 def initialize(nb_workers, max_pending_tasks=nil) @nb_workers = nb_workers if max_pending_tasks.nil? @wq = Queue.new else @wq = SizedQueue.new(max_pending_tasks) end @channels_mutex = Mutex.new @channels = {} @channelsQ = Array.new(@nb_workers){ Queue.new } end |
Instance Method Details
#do(channel = nil, *args, &task) ⇒ Object
28 29 30 |
# File 'lib/vuf/working_pool.rb', line 28 def do(channel=nil, *args, &task) @wq.push([channel,task,args]) end |
#finalize ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/vuf/working_pool.rb', line 32 def finalize return if @workers.nil? @nb_workers.times do @wq.push(ENDING_TASK) end @workers.each do |worker| worker.join end @workers=nil end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/vuf/working_pool.rb', line 17 def run if @workers.nil? @workers=[] @nb_workers.times do @workers << Thread.new do works end end end end |