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
36 37 38 |
# File 'lib/llm/function/fiber/task.rb', line 36 def alive? @fiber&.alive? || false end |
#group_class ⇒ Class
60 61 62 |
# File 'lib/llm/function/fiber/task.rb', line 60 def group_class LLM::Function::Fiber::Group end |
#interrupt! ⇒ nil Also known as: cancel!
42 43 44 45 46 |
# File 'lib/llm/function/fiber/task.rb', line 42 def interrupt! @fiber&.raise(LLM::Interrupt) if @fiber&.alive? function.interrupt! nil end |
#spawn ⇒ nil
24 25 26 27 28 29 30 31 32 |
# File 'lib/llm/function/fiber/task.rb', line 24 def spawn return if @guarded 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
51 52 53 54 55 |
# File 'lib/llm/function/fiber/task.rb', line 51 def wait return @guarded if @guarded spawn unless @fiber @result ||= @fiber.value end |