Class: Thread::Pipe::Task
- Inherits:
-
Object
- Object
- Thread::Pipe::Task
- Defined in:
- lib/thread/pipe.rb
Overview
A task incapsulates a part of the pipe.
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
-
#output ⇒ Object
Returns the value of attribute output.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check if the task has nothing to do.
-
#initialize(func, input = Queue.new, output = Queue.new) ⇒ Task
constructor
Create a Task which will call the passed function and get input from the optional parameter and put output in the optional parameter.
-
#kill ⇒ Object
Stop the task.
Constructor Details
#initialize(func, input = Queue.new, output = Queue.new) ⇒ Task
Create a Task which will call the passed function and get input from the optional parameter and put output in the optional parameter.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/thread/pipe.rb', line 23 def initialize (func, input = Queue.new, output = Queue.new) @input = input @output = output @handling = false @thread = Thread.new { while true value = @input.deq @handling = true begin value = func.call(value) @output.enq value rescue Exception; end @handling = false end } end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
19 20 21 |
# File 'lib/thread/pipe.rb', line 19 def input @input end |
#output ⇒ Object
Returns the value of attribute output.
19 20 21 |
# File 'lib/thread/pipe.rb', line 19 def output @output end |
Instance Method Details
#empty? ⇒ Boolean
Check if the task has nothing to do.
43 44 45 |
# File 'lib/thread/pipe.rb', line 43 def empty? !@handling && @input.empty? && @output.empty? end |
#kill ⇒ Object
Stop the task.
48 49 50 |
# File 'lib/thread/pipe.rb', line 48 def kill @thread.raise end |