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
48 49 50 |
# File 'lib/llm/function/async/task.rb', line 48 def alive? @alive || false end |
#group_class ⇒ Class
79 80 81 |
# File 'lib/llm/function/async/task.rb', line 79 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.
56 57 58 59 60 61 62 |
# File 'lib/llm/function/async/task.rb', line 56 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 |
# File 'lib/llm/function/async/task.rb', line 34 def spawn @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.
68 69 70 71 72 73 74 |
# File 'lib/llm/function/async/task.rb', line 68 def wait spawn unless @queue result = @queue.pop @alive = false raise result if LLM::Interrupt === result result end |