Class: Concurrently::Proc::Evaluation
- Inherits:
-
Evaluation
- Object
- Evaluation
- Concurrently::Proc::Evaluation
- Defined in:
- lib/all/concurrently/proc/evaluation.rb,
lib/all/concurrently/proc/evaluation/error.rb
Overview
Evaluations are not thread safe. They are operating on a fiber. Fibers cannot be resumed inside a thread they were not created in.
Concurrently::Proc::Evaluation
represents the evaluation of a concurrent
proc.
An instance will be returned by Evaluation.current if called by code inside a concurrent proc. It will also be returned by every call of #call_detached and also by #call_nonblock if the evaluation cannot be concluded in one go and needs to wait.
Defined Under Namespace
Modules: RescueableError Classes: Cancelled
Instance Attribute Summary collapse
-
#concluded? ⇒ Boolean
readonly
Checks if the evaluation is concluded.
Attributes inherited from Evaluation
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieves the attached value under the given key.
-
#[]=(key, value) ⇒ value
Attaches a value to the evaluation under the given key.
-
#await_result(opts = {}) ⇒ Object
Waits for the evaluation to be concluded with a result.
-
#conclude_to(result) ⇒ :concluded
Cancels the concurrent evaluation prematurely by injecting a result.
-
#key?(key) ⇒ Boolean
Checks if there is an attached value for the given key.
-
#keys ⇒ Array
Returns all keys with values.
-
#resume! ⇒ Object
Schedules the evaluation to be resumed.
Methods inherited from Evaluation
Instance Attribute Details
#concluded? ⇒ Boolean (readonly)
Checks if the evaluation is concluded
198 199 200 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 198 def concluded? @concluded end |
Instance Method Details
#[](key) ⇒ Object
Retrieves the attached value under the given key
48 49 50 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 48 def [](key) @data[key] end |
#[]=(key, value) ⇒ value
Attaches a value to the evaluation under the given key
35 36 37 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 35 def []=(key, value) @data[key] = value end |
#await_result(opts = {}) ⇒ Object #await_result(opts = {}) {|result| ... } ⇒ Object
Waits for the evaluation to be concluded with a result.
The result can be awaited from multiple places at once. All of them are resumed once the result is available.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 173 def await_result(opts = {}) # &with_result if @concluded result = @result else result = begin evaluation = Concurrently::Evaluation.current @awaiting_result.store evaluation, false await_resume! opts rescue Exception => error error ensure @awaiting_result.delete evaluation end end result = yield result if block_given? (Exception === result) ? (raise result) : result end |
#conclude_to(result) ⇒ :concluded
Cancels the concurrent evaluation prematurely by injecting a result.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 229 def conclude_to(result) if @concluded raise self.class::Error, "already concluded" end @result = result @concluded = true if Fiber.current != @fiber # Cancel its fiber by resuming it with itself as argument @fiber.resume Cancelled end @awaiting_result.each{ |evaluation, override| evaluation.resume! (override or result) } :concluded end |
#key?(key) ⇒ Boolean
Checks if there is an attached value for the given key
62 63 64 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 62 def key?(key) @data.key? key end |
#keys ⇒ Array
Returns all keys with values
75 76 77 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 75 def keys @data.keys end |
#resume! ⇒ Object
Schedules the evaluation to be resumed
For details see: Evaluation#resume!
251 252 253 254 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 251 def resume!(*) raise Evaluation::Error, "already concluded to #{@result.inspect}" if @concluded super end |