Class: CodeGrinder::Processor
- Inherits:
-
Queue
- Object
- Queue
- CodeGrinder::Processor
- Defined in:
- lib/codegrinder/processor.rb
Overview
A Processor object is the association of a queue, and one or several threads that pop what is inserted into the queue, and process it according to the block passed at initialization time.
You want a queue processed by several threads ? CodeGrinder::Processor is the guy.
Constant Summary collapse
- DEFAULT_WORKER_COUNT =
1
Instance Method Summary collapse
-
#initialize(&block) ⇒ Processor
constructor
Initialize a new CodeGrinder::Processor.
- #running? ⇒ Boolean
-
#start(nb_workers = DEFAULT_WORKER_COUNT) ⇒ Object
Start the code grinder processor.
-
#stop ⇒ Object
Stop the code grinder processor.
Constructor Details
#initialize(&block) ⇒ Processor
Initialize a new CodeGrinder::Processor. Parameters:
-
block: a proc applied to all elements inserted into the grinder.
25 26 27 28 29 30 |
# File 'lib/codegrinder/processor.rb', line 25 def initialize(&block) super() self.block = block || fail(RuntimError, 'Missing block') self.threads = WorkerThreadSet.null_object self.running = false end |
Instance Method Details
#running? ⇒ Boolean
32 33 34 |
# File 'lib/codegrinder/processor.rb', line 32 def running? running end |
#start(nb_workers = DEFAULT_WORKER_COUNT) ⇒ Object
Start the code grinder processor. Parameters:
-
ng_workers: the number of parallel threads to use for that grinder.
Return: self.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/codegrinder/processor.rb', line 43 def start(nb_workers = DEFAULT_WORKER_COUNT) fail('Cannot start a running thread') if running? self.threads = WorkerThreadSet.new(nb_workers) do # using self as a end of job marker # (inserted in the stop method) while ((args = pop) != self) block.call(*args) end end self.running = true self end |
#stop ⇒ Object
Stop the code grinder processor. Return: self.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/codegrinder/processor.rb', line 59 def stop return self unless running? # using self as a end of job marker # (tested in the start method) threads.count.times { self << self } threads.join self.threads = WorkerThreadSet.null_object self.running = false self end |