Class: Async::Task

Inherits:
Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/async/task.rb

Overview

A task represents the state associated with the execution of an asynchronous block.

Instance Attribute Summary collapse

Attributes inherited from Node

#annotation, #children, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#annotate, #consume, #description, #print_hierarchy, #reap, #traverse

Constructor Details

#initialize(reactor, parent = Task.current?) ⇒ Task

Create a new task.

Parameters:

  • reactor (Async::Reactor)

    the reactor this task will run within.

  • parent (Async::Task) (defaults to: Task.current?)

    the parent task.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/async/task.rb', line 60

def initialize(reactor, parent = Task.current?)
  super(parent || reactor)
  
  @reactor = reactor
  
  @status = :initialized
  @result = nil
  
  @finished = nil
  
  @fiber = Fiber.new do |*args|
    set!
    
    begin
      @result = yield(self, *args)
      @status = :complete
      # Async.logger.debug("Task #{self} completed normally.")
    rescue Stop
      @status = :stop
      # Async.logger.debug("Task #{self} stopped: #{$!}")
      Async.logger.debug(self) {$!}
    rescue Exception => error
      @result = error
      @status = :failed
      Async.logger.debug(self) {$!}
      raise
    ensure
      # Async.logger.debug("Task #{self} closing: #{$!}")
      finish!
    end
  end
end

Instance Attribute Details

#fiberObject (readonly)



107
108
109
# File 'lib/async/task.rb', line 107

def fiber
  @fiber
end

#reactorObject (readonly)



98
99
100
# File 'lib/async/task.rb', line 98

def reactor
  @reactor
end

#statusObject (readonly)



111
112
113
# File 'lib/async/task.rb', line 111

def status
  @status
end

Class Method Details

.currentAsync::Task

Lookup the Async::Task for the current fiber. Raise RuntimeError if none is available.

Returns:

Raises:

  • (RuntimeError)

    if task was not #set! for the current fiber.



160
161
162
# File 'lib/async/task.rb', line 160

def self.current
  Thread.current[:async_task] or raise RuntimeError, "No async task available!"
end

.current?Async::Task?

Check if there is a task defined for the current fiber.

Returns:



166
167
168
# File 'lib/async/task.rb', line 166

def self.current?
  Thread.current[:async_task]
end

.yield {|result| ... } ⇒ Object

Yield the unerlying result for the task. If the result is an Exception, then that result will be raised an its exception.

Yields:

  • (result)

    result of the task if a block if given.

Returns:

  • (Object)

    result of the task

Raises:

  • (Exception)

    if the result is an exception



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/task.rb', line 43

def self.yield
  if block_given?
    result = yield
  else
    result = Fiber.yield
  end
  
  if result.is_a? Exception
    raise result
  else
    return result
  end
end

Instance Method Details

#async(*args, &block) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/async/task.rb', line 123

def async(*args, &block)
  task = Task.new(@reactor, self, &block)
  
  task.run(*args)
  
  return task
end

#finished?Boolean

Whether we can remove this node from the reactor graph.

Returns:

  • (Boolean)


178
179
180
# File 'lib/async/task.rb', line 178

def finished?
  super && @status != :running
end

#resultObject Also known as: wait

Retrieve the current result of the task. Will cause the caller to wait until result is available.

Returns:

  • (Object)

Raises:

  • (RuntimeError)

    if the task's fiber is the current fiber.



134
135
136
137
138
139
140
141
142
143
# File 'lib/async/task.rb', line 134

def result
  raise RuntimeError.new("Cannot wait on own fiber") if Fiber.current.equal?(@fiber)
  
  if running?
    @finished ||= Condition.new
    @finished.wait
  else
    Task.yield {@result}
  end
end

#run(*args) ⇒ Object

Resume the execution of the task.



114
115
116
117
118
119
120
121
# File 'lib/async/task.rb', line 114

def run(*args)
  if @status == :initialized
    @status = :running
    @fiber.resume(*args)
  else
    raise RuntimeError, "Task already running!"
  end
end

#running?Boolean

Check if the task is running.

Returns:

  • (Boolean)


172
173
174
# File 'lib/async/task.rb', line 172

def running?
  @status == :running
end

#stopvoid

This method returns an undefined value.

Stop the task and all of its children.



149
150
151
152
153
154
155
# File 'lib/async/task.rb', line 149

def stop
  @children.each(&:stop)
  
  if @fiber.alive?
    @fiber.resume(Stop.new)
  end
end

#to_sObject



93
94
95
# File 'lib/async/task.rb', line 93

def to_s
  "<#{self.description} status=#{@status}>"
end

#yieldObject

Yield back to the reactor and allow other fibers to execute.



102
103
104
# File 'lib/async/task.rb', line 102

def yield
  reactor.yield
end