Class: LLM::Function::Async::Reactor

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

Overview

Manages an Async::Reactor on a background thread. Work is submitted through a thread-safe queue and run inside the reactor. The reactor and its fibers stay on one thread.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Reactor

Returns a new instance of Reactor.



13
14
15
16
# File 'lib/llm/function/async/reactor.rb', line 13

def initialize
  @inbox = Queue.new
  @thread = ::Thread.new { run }
end

Instance Attribute Details

#thread ⇒ Thread (readonly)

Returns:



11
12
13
# File 'lib/llm/function/async/reactor.rb', line 11

def thread
  @thread
end

Instance Method Details

#submit(&block) ⇒ nil

Submit a block to run inside the reactor.

Returns:

  • (nil)


21
22
23
24
# File 'lib/llm/function/async/reactor.rb', line 21

def submit(&block)
  @inbox << block
  nil
end

#stop ⇒ nil

Stop the reactor and wait for the thread to finish.

Called from a task's wait and from a group's ensure, so it has to survive being called twice: a second :stop is a message the loop has already gone, and a join on a finished thread returns at once.

The message is sent before the thread is compared, because a reactor stopped from its own thread - a tool that cancels the task it is running in - still has to be stopped. Only the join would wait for itself, and only the join is skipped.

Returns:

  • (nil)


38
39
40
41
42
43
44
# File 'lib/llm/function/async/reactor.rb', line 38

def stop
  @inbox << :stop
  return nil if @thread == ::Thread.current
  @thread.join(5)
  @thread.kill if @thread.alive?
  nil
end