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
(also: #concluded?)
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.
Methods inherited from Evaluation
Instance Attribute Details
#concluded ⇒ Boolean (readonly) Also known as: concluded?
Checks if the evaluation is concluded
237 238 239 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 237 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.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 216 def await_result(opts = {}) # &with_result result = if @concluded @result else begin Concurrently::Evaluation.current.__await_result_of__ self, opts rescue Exception => error error 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.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/all/concurrently/proc/evaluation.rb', line 268 def conclude_to(result) if @concluded raise self.class::Error, "already concluded\n#{Debug.notice_for @fiber}" end @result = result @concluded = true if Fiber.current != @fiber # Cancel its fiber run_queue = Concurrently::EventLoop.current.run_queue previous_evaluation = run_queue.current_evaluation run_queue.current_evaluation = self @fiber.resume Cancelled run_queue.current_evaluation = previous_evaluation end @waiting_evaluations.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 |