Class: LLM::Function::Fiber::Task

Inherits:
Task
  • Object
show all
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

#function

Instance Method Summary collapse

Constructor Details

#initialize(fn, options = {}) ⇒ Task

Returns a new instance of Task.

Parameters:



18
19
20
# File 'lib/llm/function/fiber/task.rb', line 18

def initialize(fn, options = {})
  super
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/llm/function/fiber/task.rb', line 35

def alive?
  @fiber&.alive? || false
end

#group_classClass

Returns:

  • (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!

Returns:

  • (nil)


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

#spawnnil

Returns:

  • (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

#waitLLM::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