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
- #reactor ⇒ Object readonly
- #status ⇒ Object readonly
Attributes inherited from Node
#annotation, #children, #parent
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(*args, **options, &block) ⇒ 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, &block) ⇒ Task
constructor
Create a new task.
- #logger ⇒ Object
-
#run(*args) ⇒ Object
Begin the execution of the task.
-
#running? ⇒ Boolean
Check if the task is running.
-
#stop ⇒ 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, #consume, #description, #print_hierarchy, #reap, #traverse
Constructor Details
#initialize(reactor, parent = Task.current?, logger: nil, &block) ⇒ Task
Create a new task.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/async/task.rb', line 60 def initialize(reactor, parent = Task.current?, logger: nil, &block) super(parent || reactor) @reactor = reactor @status = :initialized @result = nil @finished = nil @logger = logger @fiber = make_fiber(&block) end |
Instance Attribute Details
#fiber ⇒ Object (readonly)
92 93 94 |
# File 'lib/async/task.rb', line 92 def fiber @fiber end |
#reactor ⇒ Object (readonly)
83 84 85 |
# File 'lib/async/task.rb', line 83 def reactor @reactor end |
#status ⇒ Object (readonly)
99 100 101 |
# File 'lib/async/task.rb', line 99 def status @status end |
Class Method Details
.current ⇒ Async::Task
Lookup the Async::Task for the current fiber. Raise RuntimeError
if none is available.
159 160 161 |
# File 'lib/async/task.rb', line 159 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.
165 166 167 |
# File 'lib/async/task.rb', line 165 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.
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
#alive? ⇒ Boolean
94 95 96 |
# File 'lib/async/task.rb', line 94 def alive? @fiber&.alive? end |
#async(*args, **options, &block) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/async/task.rb', line 111 def async(*args, **, &block) task = Task.new(@reactor, self, **, &block) task.run(*args) return task end |
#complete? ⇒ Boolean
197 198 199 |
# File 'lib/async/task.rb', line 197 def complete? @status == :complete end |
#current? ⇒ Boolean
169 170 171 |
# File 'lib/async/task.rb', line 169 def current? self.equal?(Thread.current[:async_task]) end |
#failed? ⇒ Boolean
185 186 187 |
# File 'lib/async/task.rb', line 185 def failed? @status == :failed end |
#finished? ⇒ Boolean
Whether we can remove this node from the reactor graph.
181 182 183 |
# File 'lib/async/task.rb', line 181 def finished? super && @status != :running end |
#logger ⇒ Object
78 79 80 |
# File 'lib/async/task.rb', line 78 def logger @logger ||= @parent&.logger end |
#run(*args) ⇒ Object
Begin the execution of the task.
102 103 104 105 106 107 108 109 |
# File 'lib/async/task.rb', line 102 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.
175 176 177 |
# File 'lib/async/task.rb', line 175 def running? @status == :running end |
#stop ⇒ void
This method returns an undefined value.
Stop the task and all of its children.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/async/task.rb', line 139 def stop if self.stopping? # If we are already stopping this task... don't try to stop it again. return true elsif self.running? @status = :stopping if self.current? raise Stop, "Stopping current fiber!" elsif @fiber&.alive? @fiber.resume(Stop.new) end end ensure @children&.each(&:stop) end |
#stopped? ⇒ Boolean
193 194 195 |
# File 'lib/async/task.rb', line 193 def stopped? @status == :stopped end |
#stopping? ⇒ Boolean
189 190 191 |
# File 'lib/async/task.rb', line 189 def stopping? @status == :stopping end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/async/task.rb', line 74 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.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/async/task.rb', line 122 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 |
#yield ⇒ Object
Yield back to the reactor and allow other fibers to execute.
87 88 89 |
# File 'lib/async/task.rb', line 87 def yield reactor.yield end |