Class: SilentWorker
- Inherits:
-
Object
- Object
- SilentWorker
- Defined in:
- lib/silent_worker.rb,
lib/silent_worker/version.rb
Constant Summary collapse
- FINISH_DATA =
EOT
"\x04"- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#job ⇒ Object
readonly
Returns the value of attribute job.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #abort ⇒ Object
-
#initialize(workers = 8, &job) ⇒ SilentWorker
constructor
A new instance of SilentWorker.
- #start ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(workers = 8, &job) ⇒ SilentWorker
Returns a new instance of SilentWorker.
9 10 11 12 13 14 15 |
# File 'lib/silent_worker.rb', line 9 def initialize(workers = 8, &job) @job = job @workers = workers @threads = [] @queue = Queue.new start end |
Instance Attribute Details
#job ⇒ Object (readonly)
Returns the value of attribute job.
5 6 7 |
# File 'lib/silent_worker.rb', line 5 def job @job end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
5 6 7 |
# File 'lib/silent_worker.rb', line 5 def queue @queue end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
5 6 7 |
# File 'lib/silent_worker.rb', line 5 def workers @workers end |
Instance Method Details
#<<(data) ⇒ Object
17 18 19 |
# File 'lib/silent_worker.rb', line 17 def <<(data) @queue.push(data) end |
#abort ⇒ Object
29 30 31 32 |
# File 'lib/silent_worker.rb', line 29 def abort @threads.each(&:kill) wait end |
#start ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/silent_worker.rb', line 38 def start @workers.times do @threads << Thread.start(@job, @queue) do |job, queue| loop do data = queue.pop break if @finished && data == FINISH_DATA job.call(data) end end end end |
#stop ⇒ Object
34 35 36 |
# File 'lib/silent_worker.rb', line 34 def stop wait end |
#wait ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/silent_worker.rb', line 21 def wait finish! @workers.times do @queue.push(FINISH_DATA) end @threads.each(&:join) end |