Class: Async::Scheduler
Overview
Handles scheduling of fibers. Implements the fiber scheduler interface.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Node
#annotation, #children, #head, #parent, #tail
Class Method Summary collapse
-
.supported? ⇒ Boolean
Whether the fiber scheduler is supported.
Instance Method Summary collapse
- #address_resolve(hostname) ⇒ Object
-
#async(*arguments, **options, &block) ⇒ Object
deprecated
Deprecated.
With no replacement.
-
#block(blocker, timeout) ⇒ Object
Invoked when a fiber tries to perform a blocking operation which cannot continue.
- #close ⇒ Object
- #closed? ⇒ Boolean
- #fiber ⇒ Object
-
#initialize(parent = nil, selector: nil) ⇒ Scheduler
constructor
A new instance of Scheduler.
-
#interrupt ⇒ Object
Interrupt the event loop and cause it to exit.
- #io_read(io, buffer, length) ⇒ Object
- #io_wait(io, events, timeout = nil) ⇒ Object
- #io_write(io, buffer, length) ⇒ Object
- #kernel_sleep(duration = nil) ⇒ Object
-
#process_wait(pid, flags) ⇒ Object
Wait for the specified process ID to exit.
-
#push(fiber) ⇒ Object
Schedule a fiber (or equivalent object) to be resumed on the next loop through the reactor.
- #raise(*arguments) ⇒ Object
- #resume(fiber, *arguments) ⇒ Object
-
#run ⇒ Object
Run the reactor until all tasks are finished.
-
#run_once(timeout = nil) ⇒ Object
Run one iteration of the event loop.
- #timeout_after(duration, exception, message, &block) ⇒ Object
- #to_s ⇒ Object
-
#transfer ⇒ Object
Transfer from the calling fiber to the event loop.
- #unblock(blocker, fiber) ⇒ Object
-
#with_timeout(duration, exception = TimeoutError, message = "execution expired", &block) ⇒ Object
Invoke the block, but after the specified timeout, raise TimeoutError in any currenly blocking operation.
-
#yield ⇒ Object
Yield the current fiber and resume it on the next iteration of the event loop.
Methods inherited from Node
#The parent node.=, #annotate, #backtrace, #children?, #consume, #description, #finished?, #print_hierarchy, #root, #stop, #stopped?, #terminate, #transient?, #traverse
Constructor Details
#initialize(parent = nil, selector: nil) ⇒ Scheduler
Returns a new instance of Scheduler.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/async/scheduler.rb', line 39 def initialize(parent = nil, selector: nil) super(parent) @selector = selector || ::IO::Event::Selector.new(Fiber.current) @interrupted = false @blocked = 0 @timers = ::Timers::Group.new end |
Class Method Details
.supported? ⇒ Boolean
Whether the fiber scheduler is supported.
35 36 37 |
# File 'lib/async/scheduler.rb', line 35 def self.supported? true end |
Instance Method Details
#address_resolve(hostname) ⇒ Object
155 156 157 |
# File 'lib/async/scheduler.rb', line 155 def address_resolve(hostname) ::Resolv.getaddresses(hostname) end |
#async(*arguments, **options, &block) ⇒ Object
With no replacement.
Start an asynchronous task within the specified reactor. The task will be executed until the first blocking call, at which point it will yield and and this method will return.
This is the main entry point for scheduling asynchronus tasks.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/async/scheduler.rb', line 263 def async(*arguments, **, &block) task = Task.new(Task.current? || self, **, &block) # I want to take a moment to explain the logic of this. # When calling an async block, we deterministically execute it until the # first blocking operation. We don't *have* to do this - we could schedule # it for later execution, but it's useful to: # - Fail at the point of the method call where possible. # - Execute determinstically where possible. # - Avoid scheduler overhead if no blocking operation is performed. task.run(*arguments) # Console.logger.debug "Initial execution of task #{fiber} complete (#{result} -> #{fiber.alive?})..." return task end |
#block(blocker, timeout) ⇒ Object
Invoked when a fiber tries to perform a blocking operation which cannot continue. A corresponding call #unblock must be performed to allow this fiber to continue.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/async/scheduler.rb', line 114 def block(blocker, timeout) # $stderr.puts "block(#{blocker}, #{Fiber.current}, #{timeout})" fiber = Fiber.current if timeout timer = @timers.after(timeout) do if fiber.alive? fiber.transfer(false) end end end begin @blocked += 1 @selector.transfer ensure @blocked -= 1 end ensure timer&.cancel end |
#close ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/async/scheduler.rb', line 51 def close # This is a critical step. Because tasks could be stored as instance variables, and since the reactor is (probably) going out of scope, we need to ensure they are stopped. Otherwise, the tasks will belong to a reactor that will never run again and are not stopped. self.terminate Kernel::raise "Closing scheduler with blocked operations!" if @blocked > 0 # We depend on GVL for consistency: # @guard.synchronize do @selector&.close @selector = nil # end consume end |
#closed? ⇒ Boolean
70 71 72 |
# File 'lib/async/scheduler.rb', line 70 def closed? @selector.nil? end |
#fiber ⇒ Object
279 280 281 |
# File 'lib/async/scheduler.rb', line 279 def fiber(...) return async(...).fiber end |
#interrupt ⇒ Object
Interrupt the event loop and cause it to exit.
79 80 81 82 |
# File 'lib/async/scheduler.rb', line 79 def interrupt @interrupted = true @selector.wakeup end |
#io_read(io, buffer, length) ⇒ Object
181 182 183 |
# File 'lib/async/scheduler.rb', line 181 def io_read(io, buffer, length) @selector.io_read(Fiber.current, io, buffer, length) end |
#io_wait(io, events, timeout = nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/async/scheduler.rb', line 160 def io_wait(io, events, timeout = nil) fiber = Fiber.current if timeout timer = @timers.after(timeout) do fiber.raise(TimeoutError) end end # Console.logger.info(self, "-> io_wait", fiber, io, events) events = @selector.io_wait(fiber, io, events) # Console.logger.info(self, "<- io_wait", fiber, io, events) return events rescue TimeoutError return false ensure timer&.cancel end |
#io_write(io, buffer, length) ⇒ Object
185 186 187 |
# File 'lib/async/scheduler.rb', line 185 def io_write(io, buffer, length) @selector.io_write(Fiber.current, io, buffer, length) end |
#kernel_sleep(duration = nil) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/async/scheduler.rb', line 146 def kernel_sleep(duration = nil) if duration self.block(nil, duration) else self.transfer end end |
#process_wait(pid, flags) ⇒ Object
Wait for the specified process ID to exit.
195 196 197 |
# File 'lib/async/scheduler.rb', line 195 def process_wait(pid, flags) return @selector.process_wait(Fiber.current, pid, flags) end |
#push(fiber) ⇒ Object
Schedule a fiber (or equivalent object) to be resumed on the next loop through the reactor.
96 97 98 |
# File 'lib/async/scheduler.rb', line 96 def push(fiber) @selector.push(fiber) end |
#raise(*arguments) ⇒ Object
100 101 102 |
# File 'lib/async/scheduler.rb', line 100 def raise(*arguments) @selector.raise(*arguments) end |
#resume(fiber, *arguments) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/async/scheduler.rb', line 104 def resume(fiber, *arguments) if Fiber.scheduler @selector.resume(fiber, *arguments) else @selector.push(fiber) end end |
#run ⇒ Object
Run the reactor until all tasks are finished. Proxies arguments to #async immediately before entering the loop, if a block is provided.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/async/scheduler.rb', line 236 def run(...) Kernel::raise RuntimeError, 'Reactor has been closed' if @selector.nil? initial_task = self.async(...) if block_given? @interrupted = false while self.run_once if @interrupted break end end return initial_task ensure Console.logger.debug(self) {"Exiting run-loop because #{$! ? $! : 'finished'}."} end |
#run_once(timeout = nil) ⇒ Object
Run one iteration of the event loop.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/async/scheduler.rb', line 202 def run_once(timeout = nil) Kernel::raise "Running scheduler on non-blocking fiber!" unless Fiber.blocking? # If we are finished, we stop the task tree and exit: if self.finished? return false end interval = @timers.wait_interval # If there is no interval to wait (thus no timers), and no tasks, we could be done: if interval.nil? # Allow the user to specify a maximum interval if we would otherwise be sleeping indefinitely: interval = timeout elsif interval < 0 # We have timers ready to fire, don't sleep in the selctor: interval = 0 elsif timeout and interval > timeout interval = timeout end begin @selector.select(interval) rescue Errno::EINTR # Ignore. end @timers.fire # The reactor still has work to do: return true end |
#timeout_after(duration, exception, message, &block) ⇒ Object
299 300 301 302 303 |
# File 'lib/async/scheduler.rb', line 299 def timeout_after(duration, exception, , &block) with_timeout(duration, exception, ) do |timer| yield duration end end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/async/scheduler.rb', line 74 def to_s "\#<#{self.description} #{@children&.size || 0} children (#{stopped? ? 'stopped' : 'running'})>" end |
#transfer ⇒ Object
Transfer from the calling fiber to the event loop.
85 86 87 |
# File 'lib/async/scheduler.rb', line 85 def transfer @selector.transfer end |
#unblock(blocker, fiber) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/async/scheduler.rb', line 137 def unblock(blocker, fiber) # $stderr.puts "unblock(#{blocker}, #{fiber})" # This operation is protected by the GVL: @selector.push(fiber) @selector.wakeup end |
#with_timeout(duration, exception = TimeoutError, message = "execution expired", &block) ⇒ Object
Invoke the block, but after the specified timeout, raise TimeoutError in any currenly blocking operation. If the block runs to completion before the timeout occurs or there are no non-blocking operations after the timeout expires, the code will complete without any exception.
285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/async/scheduler.rb', line 285 def with_timeout(duration, exception = TimeoutError, = "execution expired", &block) fiber = Fiber.current timer = @timers.after(duration) do if fiber.alive? fiber.raise(exception, ) end end yield timer ensure timer.cancel if timer end |
#yield ⇒ Object
Yield the current fiber and resume it on the next iteration of the event loop.
90 91 92 |
# File 'lib/async/scheduler.rb', line 90 def yield @selector.yield end |