Class: Async::Task
- 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
- #fiber ⇒ Object readonly
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
- #reactor ⇒ Object readonly
- #status ⇒ Object readonly
Attributes inherited from Node
#annotation, #children, #head, #parent, #tail
Class Method Summary collapse
-
.current ⇒ Async::Task
Lookup the Task for the current fiber.
-
.current? ⇒ Async::Task?
Check if there is a task defined for the current fiber.
-
.yield {|result| ... } ⇒ Object
Yield the unerlying ‘result` for the task.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #async(*arguments, **options, &block) ⇒ Object
- #backtrace(*arguments) ⇒ Object
- #complete? ⇒ Boolean
- #current? ⇒ Boolean
- #failed? ⇒ Boolean
-
#finished? ⇒ Boolean
Whether we can remove this node from the reactor graph.
-
#initialize(reactor, parent = Task.current?, logger: nil, finished: nil, **options, &block) ⇒ Task
constructor
Create a new task.
-
#run(*arguments) ⇒ Object
Begin the execution of the task.
-
#running? ⇒ Boolean
Check if the task is running.
-
#stop(later = false) ⇒ void
Stop the task and all of its children.
- #stopped? ⇒ Boolean
- #stopping? ⇒ Boolean
- #to_s ⇒ Object
-
#wait ⇒ Object
(also: #result)
Retrieve the current result of the task.
-
#yield ⇒ Object
Yield back to the reactor and allow other fibers to execute.
Methods inherited from Node
#annotate, #children?, #consume, #description, #print_hierarchy, #transient?, #traverse
Constructor Details
#initialize(reactor, parent = Task.current?, logger: nil, finished: nil, **options, &block) ⇒ Task
Create a new task.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/async/task.rb', line 75 def initialize(reactor, parent = Task.current?, logger: nil, finished: nil, **, &block) super(parent || reactor, **) @reactor = reactor @status = :initialized @result = nil @finished = finished @logger = logger || @parent.logger @fiber = make_fiber(&block) end |
Instance Attribute Details
#fiber ⇒ Object (readonly)
112 113 114 |
# File 'lib/async/task.rb', line 112 def fiber @fiber end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
89 90 91 |
# File 'lib/async/task.rb', line 89 def logger @logger end |
#reactor ⇒ Object (readonly)
102 103 104 |
# File 'lib/async/task.rb', line 102 def reactor @reactor end |
#status ⇒ Object (readonly)
119 120 121 |
# File 'lib/async/task.rb', line 119 def status @status end |
Class Method Details
.current ⇒ Async::Task
Lookup the Async::Task for the current fiber. Raise ‘RuntimeError` if none is available.
189 190 191 |
# File 'lib/async/task.rb', line 189 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.
195 196 197 |
# File 'lib/async/task.rb', line 195 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/async/task.rb', line 58 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
#alive? ⇒ Boolean
114 115 116 |
# File 'lib/async/task.rb', line 114 def alive? @fiber&.alive? end |
#async(*arguments, **options, &block) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/async/task.rb', line 132 def async(*arguments, **, &block) task = Task.new(@reactor, self, **, &block) task.run(*arguments) return task end |
#backtrace(*arguments) ⇒ Object
92 93 94 |
# File 'lib/async/task.rb', line 92 def backtrace(*arguments) @fiber&.backtrace(*arguments) end |
#complete? ⇒ Boolean
227 228 229 |
# File 'lib/async/task.rb', line 227 def complete? @status == :complete end |
#current? ⇒ Boolean
199 200 201 |
# File 'lib/async/task.rb', line 199 def current? self.equal?(Thread.current[:async_task]) end |
#failed? ⇒ Boolean
215 216 217 |
# File 'lib/async/task.rb', line 215 def failed? @status == :failed end |
#finished? ⇒ Boolean
Whether we can remove this node from the reactor graph.
211 212 213 |
# File 'lib/async/task.rb', line 211 def finished? super && @status != :running end |
#run(*arguments) ⇒ Object
Begin the execution of the task.
122 123 124 125 126 127 128 129 130 |
# File 'lib/async/task.rb', line 122 def run(*arguments) if @status == :initialized @status = :running @fiber.resume(*arguments) else raise RuntimeError, "Task already running!" end end |
#running? ⇒ Boolean
Check if the task is running.
205 206 207 |
# File 'lib/async/task.rb', line 205 def running? @status == :running end |
#stop(later = false) ⇒ void
This method returns an undefined value.
Stop the task and all of its children.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/async/task.rb', line 160 def stop(later = false) if self.stopped? # If we already stopped this task... don't try to stop it again: return end if self.running? if self.current? if later @reactor << Stop::Later.new(self) else raise Stop, "Stopping current task!" end elsif @fiber&.alive? begin @fiber.resume(Stop.new) rescue FiberError @reactor << Stop::Later.new(self) end end else # We are not running, but children might be, so transition directly into stopped state: stop! end end |
#stopped? ⇒ Boolean
223 224 225 |
# File 'lib/async/task.rb', line 223 def stopped? @status == :stopped end |
#stopping? ⇒ Boolean
219 220 221 |
# File 'lib/async/task.rb', line 219 def stopping? @status == :stopping end |
#to_s ⇒ Object
97 98 99 |
# File 'lib/async/task.rb', line 97 def to_s "\#<#{self.description} (#{@status})>" end |
#wait ⇒ Object Also known as: result
Retrieve the current result of the task. Will cause the caller to wait until result is available.
143 144 145 146 147 148 149 150 151 152 |
# File 'lib/async/task.rb', line 143 def wait raise RuntimeError, "Cannot wait on own fiber" if Fiber.current.equal?(@fiber) if running? @finished ||= Condition.new @finished.wait else Task.yield{@result} end end |