Class: Thread::Pipe::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/thread/pipe.rb

Overview

A task incapsulates a part of the pipe.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#inputObject

Returns the value of attribute input.



19
20
21
# File 'lib/thread/pipe.rb', line 19

def input
  @input
end

#outputObject

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.

Returns:

  • (Boolean)


43
44
45
# File 'lib/thread/pipe.rb', line 43

def empty?
	!@handling && @input.empty? && @output.empty?
end

#killObject

Stop the task.



48
49
50
# File 'lib/thread/pipe.rb', line 48

def kill
	@thread.raise
end