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)


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

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

#group_classClass

Returns:

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

Returns:

  • (nil)


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

#spawnnil

Returns:

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

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