Class: LLM::Function::Async::Task
- Defined in:
- lib/llm/function/async/task.rb
Overview
Instance Attribute Summary
Attributes inherited from Task
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #group_class ⇒ Class
-
#initialize(fn, options = {}) ⇒ Task
constructor
A new instance of Task.
-
#interrupt! ⇒ nil
(also: #cancel!)
Push an interrupt sentinel to the result queue.
-
#reactor=(reactor) ⇒ Object
Assign the reactor.
-
#spawn ⇒ nil
Submit the function call to the reactor.
-
#wait ⇒ LLM::Function::Return
(also: #value)
Wait for the result queue to contain a value.
Constructor Details
#initialize(fn, options = {}) ⇒ Task
Returns a new instance of Task.
17 18 19 20 |
# File 'lib/llm/function/async/task.rb', line 17 def initialize(fn, = {}) super @reactor = [:reactor] end |
Instance Method Details
#alive? ⇒ Boolean
49 50 51 |
# File 'lib/llm/function/async/task.rb', line 49 def alive? @alive || false end |
#group_class ⇒ Class
81 82 83 |
# File 'lib/llm/function/async/task.rb', line 81 def group_class LLM::Function::Async::Group end |
#interrupt! ⇒ nil Also known as: cancel!
Push an interrupt sentinel to the result queue. The reactor thread continues running but the result is discarded.
57 58 59 60 61 62 63 |
# File 'lib/llm/function/async/task.rb', line 57 def interrupt! if @queue @alive = false @queue << LLM::Interrupt.new end nil end |
#reactor=(reactor) ⇒ Object
Assign the reactor. Used when a task is created before its reactor is available.
26 27 28 |
# File 'lib/llm/function/async/task.rb', line 26 def reactor=(reactor) @reactor = reactor end |
#spawn ⇒ nil
Submit the function call to the reactor. The result is pushed to a queue that #wait consumes.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/llm/function/async/task.rb', line 34 def spawn return if @guarded @queue = Queue.new @alive = true @reactor.submit do @queue << function.call rescue LLM::Interrupt => e @queue << e raise end nil end |
#wait ⇒ LLM::Function::Return Also known as: value
Wait for the result queue to contain a value.
69 70 71 72 73 74 75 76 |
# File 'lib/llm/function/async/task.rb', line 69 def wait return @guarded if @guarded spawn unless @queue result = @queue.pop @alive = false raise result if LLM::Interrupt === result result end |