Class: LLM::Function::Async::Task

Inherits:
Task
  • Object
show all
Defined in:
lib/llm/function/async/task.rb

Overview

Task wraps a function call in an Task running on a shared Reactor. The task is spawned lazily in #wait or explicitly in #spawn.

Work is submitted to the reactor through its inbox queue. Results are bridged back through the task's own queue.

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:

Options Hash (options):



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

def initialize(fn, options = {})
  super
  @reactor = options[:reactor]
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/llm/function/async/task.rb', line 48

def alive?
  @alive || false
end

#group_classClass

Returns:

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

Returns:

  • (nil)


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.

Parameters:



26
27
28
# File 'lib/llm/function/async/task.rb', line 26

def reactor=(reactor)
  @reactor = reactor
end

#spawnnil

Submit the function call to the reactor. The result is pushed to a queue that #wait consumes.

Returns:

  • (nil)


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

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