Class: Stud::Task
- Inherits:
-
Object
- Object
- Stud::Task
- Defined in:
- lib/stud/task.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(*args, &block) ⇒ Task
constructor
A new instance of Task.
-
#stop! ⇒ Object
def wait.
-
#wait ⇒ Object
def initialize.
Constructor Details
#initialize(*args, &block) ⇒ Task
Returns a new instance of Task.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/stud/task.rb', line 5 def initialize(*args, &block) # A queue to receive the result of the block # TODO(sissel): Don't use a queue, just store it in an instance variable. @queue = Queue.new @thread = Thread.new(@queue, *args) do |queue, *args| begin result = block.call(*args) queue << [:return, result] rescue => e queue << [:exception, e] end end # thread end |
Class Method Details
.interrupted? ⇒ Boolean
36 37 38 |
# File 'lib/stud/task.rb', line 36 def self.interrupted? Thread.current[:stud_task_interrupted] end |
Instance Method Details
#stop! ⇒ Object
def wait
32 33 34 |
# File 'lib/stud/task.rb', line 32 def stop! Thread.current[:stud_task_interrupted] = true end |
#wait ⇒ Object
def initialize
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/stud/task.rb', line 20 def wait @thread.join reason, result = @queue.pop if reason == :exception #raise StandardError.new(result) raise result else return result end end |