Class: LLM::Function::Async::Reactor
- Inherits:
-
Object
- Object
- LLM::Function::Async::Reactor
- 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
- #thread ⇒ Thread readonly
Instance Method Summary collapse
-
#initialize ⇒ Reactor
constructor
A new instance of Reactor.
-
#submit(&block) ⇒ nil
Submit a block to run inside the reactor.
-
#stop ⇒ nil
Stop the reactor and wait for the thread to finish.
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)
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.
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.
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 |