Class: LLM::Function::Fiber::Task
- Defined in:
- lib/llm/function/fiber/task.rb
Overview
LLM::Function::Fiber::Task wraps a function call in a scheduler-backed fiber for cooperative concurrent execution. The fiber is created lazily when #wait is called, not at construction time.
Requires Fiber.scheduler — without one, raise early in
#wait. Interrupting a running task raises
Interrupt on the fiber, which stops it at the next
yield point.
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!)
- #spawn ⇒ nil
- #wait ⇒ LLM::Function::Return (also: #value)
Constructor Details
#initialize(fn, options = {}) ⇒ Task
Returns a new instance of Task.
18 19 20 |
# File 'lib/llm/function/fiber/task.rb', line 18 def initialize(fn, = {}) super end |
Instance Method Details
#alive? ⇒ Boolean
35 36 37 |
# File 'lib/llm/function/fiber/task.rb', line 35 def alive? @fiber&.alive? || false end |
#group_class ⇒ Class
58 59 60 |
# File 'lib/llm/function/fiber/task.rb', line 58 def group_class LLM::Function::Fiber::Group end |
#interrupt! ⇒ nil Also known as: cancel!
41 42 43 44 45 |
# File 'lib/llm/function/fiber/task.rb', line 41 def interrupt! @fiber&.raise(LLM::Interrupt) if @fiber&.alive? function.interrupt! nil end |
#spawn ⇒ nil
24 25 26 27 28 29 30 31 |
# File 'lib/llm/function/fiber/task.rb', line 24 def spawn if Fiber.scheduler.nil? raise ArgumentError, "Fiber concurrency requires Fiber.scheduler" else @fiber = Fiber.schedule { function.call } nil end end |
#wait ⇒ LLM::Function::Return Also known as: value
50 51 52 53 |
# File 'lib/llm/function/fiber/task.rb', line 50 def wait spawn unless @fiber @result ||= @fiber.value end |