AsyncSourceAsyncScheduler

class Scheduler

Handles scheduling of fibers. Implements the fiber scheduler interface.

Nested

Definitions

def self.supported?

  • public

Whether the fiber scheduler is supported.

Signature

public

Since Async v1.

Implementation

def self.supported?
	true
end

def initialize(parent = nil, selector: nil)

  • public

Create a new scheduler.

Signature

public

Since Async v1.

parameter parent Node | Nil

The parent node to use for task hierarchy.

parameter selector IO::Event::Selector

The selector to use for event handling.

Implementation

def initialize(parent = nil, selector: nil)
	super(parent)
	
	@selector = selector || ::IO::Event::Selector.new(Fiber.current)
	@interrupted = false
	
	@blocked = 0
	
	@busy_time = 0.0
	@idle_time = 0.0
	
	@timers = ::IO::Event::Timers.new
end

def load

Compute the scheduler load according to the busy and idle times that are updated by the run loop.

Signature

returns Float

The load of the scheduler. 0.0 means no load, 1.0 means fully loaded or over-loaded.

Implementation

def load
	total_time = @busy_time + @idle_time
	
	# If the total time is zero, then the load is zero:
	return 0.0 if total_time.zero?
	
	# We normalize to a 1 second window:
	if total_time > 1.0
		ratio = 1.0 / total_time
		@busy_time *= ratio
		@idle_time *= ratio
		
		# We don't need to divide here as we've already normalised it to a 1s window:
		return @busy_time
	else
		return @busy_time / total_time
	end
end

def scheduler_close(error = $!)

Invoked when the fiber scheduler is being closed.

Executes the run loop until all tasks are finished, then closes the scheduler.

Implementation

def scheduler_close(error = $!)
	# If the execution context (thread) was handling an exception, we want to exit as quickly as possible:
	unless error
		self.run
	end
ensure
	self.close
end

def terminate

Terminate all child tasks.

Implementation

def terminate
	# If that doesn't work, take more serious action:
	@children&.each do |child|
		child.terminate
	end
	
	return @children.nil?
end

def close

  • public

Terminate all child tasks and close the scheduler.

Signature

public

Since Async v1.

Implementation

def close
	self.run_loop do
		until self.terminate
			self.run_once!
		end
	end
	
	Kernel.raise "Closing scheduler with blocked operations!" if @blocked > 0
ensure
	# We want `@selector = nil` to be a visible side effect from this point forward, specifically in `#interrupt` and `#unblock`. If the selector is closed, then we don't want to push any fibers to it.
	selector = @selector
	@selector = nil
	
	selector&.close
	
	consume
end

def closed?

  • public

Signature

returns Boolean

Whether the scheduler has been closed.

public

Since Async v1.

Implementation

def closed?
	@selector.nil?
end

def to_s

Signature

returns String

A description of the scheduler.

Implementation

def to_s
	"\#<#{self.description} #{@children&.size || 0} children (#{stopped? ? 'stopped' : 'running'})>"
end

def interrupt

  • asynchronous

Interrupt the event loop and cause it to exit.

Signature

asynchronous

May be called from any thread.

Implementation

def interrupt
	@interrupted = true
	@selector&.wakeup
end

def transfer

Transfer from the calling fiber to the event loop.

Implementation

def transfer
	@selector.transfer
end

def yield

Yield the current fiber and resume it on the next iteration of the event loop.

Implementation

def yield
	@selector.yield
end

def push(fiber)

Schedule a fiber (or equivalent object) to be resumed on the next loop through the reactor.

Signature

parameter fiber Fiber | Object

The object to be resumed on the next iteration of the run-loop.

Implementation

def push(fiber)
	@selector.push(fiber)
end

def raise(...)

Raise an exception on a specified fiber with the given arguments.

This internally schedules the current fiber to be ready, before raising the exception, so that it will later resume execution.

Signature

parameter fiber Fiber

The fiber to raise the exception on.

parameter *arguments Array

The arguments to pass to the fiber.

Implementation

def raise(...)
	@selector.raise(...)
end

def resume(fiber, *arguments)

Resume execution of the specified fiber.

Signature

parameter fiber Fiber

The fiber to resume.

parameter arguments Array

The arguments to pass to the fiber.

Implementation

def resume(fiber, *arguments)
	@selector.resume(fiber, *arguments)
end

def block(blocker, timeout)

  • asynchronous

Signature

asynchronous

May only be called on same thread as fiber scheduler.

Implementation

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

def unblock(blocker, fiber)

  • public
  • asynchronous

Unblock a fiber that was previously blocked.

Signature

public

Since Async v2 and Ruby v3.1.

asynchronous

May be called from any thread.

parameter blocker Object

The object that was blocking the fiber.

parameter fiber Fiber

The fiber to unblock.

Implementation

def unblock(blocker, fiber)
	# $stderr.puts "unblock(#{blocker}, #{fiber})"
	
	# This operation is protected by the GVL:
	if selector = @selector
		selector.push(fiber)
		selector.wakeup
	end
end

def kernel_sleep(duration = nil)

  • public
  • asynchronous

Sleep for the specified duration.

Signature

public

Since Async v2 and Ruby v3.1.

asynchronous

May be non-blocking.

parameter duration Numeric | Nil

The time in seconds to sleep, or if nil, indefinitely.

Implementation

def kernel_sleep(duration = nil)
	if duration
		self.block(nil, duration)
	else
		self.transfer
	end
end

def address_resolve(hostname)

  • public
  • asynchronous

Resolve the address of the given hostname.

Signature

public

Since Async v2.

asynchronous

May be non-blocking.

parameter hostname String

The hostname to resolve.

Implementation

def address_resolve(hostname)
	# On some platforms, hostnames may contain a device-specific suffix (e.g. %en0). We need to strip this before resolving.
	# See <https://github.com/socketry/async/issues/180> for more details.
	hostname = hostname.split("%", 2).first
	::Resolv.getaddresses(hostname)
end

def io_wait(io, events, timeout = nil)

  • public
  • asynchronous

Wait for the specified IO to become ready for the specified events.

Signature

public

Since Async v2.

asynchronous

May be non-blocking.

parameter io IO

The IO object to wait on.

parameter events Integer

The events to wait for, e.g. IO::READABLE, IO::WRITABLE, etc.

parameter timeout Float | Nil

The maximum time to wait, or if nil, indefinitely.

Implementation

def io_wait(io, events, timeout = nil)
	fiber = Fiber.current
	
	if timeout
		# If an explicit timeout is specified, we expect that the user will handle it themselves:
		timer = @timers.after(timeout) do
			fiber.transfer
		end
	elsif timeout = get_timeout(io)
		# Otherwise, if we default to the io's timeout, we raise an exception:
		timer = @timers.after(timeout) do
			fiber.raise(::IO::TimeoutError, "Timeout (#{timeout}s) while waiting for IO to become ready!")
		end
	end
	
	return @selector.io_wait(fiber, io, events)
ensure
	timer&.cancel!
end

def io_read(io, buffer, length, offset = 0)

  • public
  • asynchronous

Read from the specified IO into the buffer.

Signature

public

Since Async v2 and Ruby with IO::Buffer support.

asynchronous

May be non-blocking.

parameter io IO

The IO object to read from.

parameter buffer IO::Buffer

The buffer to read into.

parameter length Integer

The minimum number of bytes to read.

parameter offset Integer

The offset within the buffer to read into.

Implementation

def io_read(io, buffer, length, offset = 0)
	fiber = Fiber.current
	
	if timeout = get_timeout(io)
		timer = @timers.after(timeout) do
			fiber.raise(::IO::TimeoutError, "Timeout (#{timeout}s) while waiting for IO to become readable!")
		end
	end
	
	@selector.io_read(fiber, io, buffer, length, offset)
ensure
	timer&.cancel!
end

def io_write(io, buffer, length, offset = 0)

  • public
  • asynchronous

Write the specified buffer to the IO.

Signature

public

Since Async v2 and Ruby v3.3.1 with IO::Buffer support.

asynchronous

May be non-blocking.

parameter io IO

The IO object to write to.

parameter buffer IO::Buffer

The buffer to write from.

parameter length Integer

The minimum number of bytes to write.

parameter offset Integer

The offset within the buffer to write from.

Implementation

def io_write(io, buffer, length, offset = 0)
	fiber = Fiber.current
	
	if timeout = get_timeout(io)
		timer = @timers.after(timeout) do
			fiber.raise(::IO::TimeoutError, "Timeout (#{timeout}s) while waiting for IO to become writable!")
		end
	end
	
	@selector.io_write(fiber, io, buffer, length, offset)
ensure
	timer&.cancel!
end

def process_wait(pid, flags)

  • public
  • asynchronous
  • asynchronous

Wait for the specified process ID to exit.

Signature

public

Since Async v2.

asynchronous

May be non-blocking.

parameter pid Integer

The process ID to wait for.

parameter flags Integer

A bit-mask of flags suitable for Process::Status.wait.

returns Process::Status

A process status instance.

asynchronous

May be non-blocking..

Implementation

def process_wait(pid, flags)
	return @selector.process_wait(Fiber.current, pid, flags)
end

def blocking_operation_wait(work)

  • public
  • asynchronous

Wait for the given work to be executed.

Signature

public

Since Async v2.19 and Ruby v3.4.

asynchronous

May be non-blocking.

parameter work Proc

The work to execute on a background thread.

returns Object

The result of the work.

Implementation

def blocking_operation_wait(work)
	thread = Thread.new(&work)
	
	result = thread.join
	
	thread = nil
	
	return result
ensure
	thread&.kill
end

def run_once(timeout = nil)

  • public
  • asynchronous

Run one iteration of the event loop.

Signature

public

Since Async v1.

asynchronous

Must be invoked from blocking (root) fiber.

parameter timeout Float | Nil

The maximum timeout, or if nil, indefinite.

returns Boolean

Whether there is more work to do.

Implementation

def run_once(timeout = nil)
	Kernel.raise "Running scheduler on non-blocking fiber!" unless Fiber.blocking?
	
	if self.finished?
		self.stop
	end
	
	# If we are finished, we stop the task tree and exit:
	if @children.nil?
		return false
	end
	
	return run_once!(timeout)
end

def stop

  • public

Stop all children, including transient children.

Signature

public

Since Async v1.

Implementation

def stop
	@children&.each do |child|
		child.stop
	end
end

def run(...)

  • public

Run the reactor until all tasks are finished. Proxies arguments to #async immediately before entering the loop, if a block is provided.

Forwards all parameters to #async if a block is given.

Signature

public

Since Async v1.

yields {|task| ...}

The top level task, if a block is given.

returns Task

The initial task that was scheduled into the reactor.

Implementation

def run(...)
	Kernel.raise ClosedError if @selector.nil?
	
	initial_task = self.async(...) if block_given?
	
	self.run_loop do
		run_once
	end
	
	return initial_task
end

def async(*arguments, **options, &block)

  • public
  • asynchronous
  • deprecated

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.

Signature

public

Since Async v1.

asynchronous

May context switch immediately to new task.

deprecated

Use #run or Async::Task#async instead.

yields {|task| ...}

Executed within the task.

returns Task

The task that was scheduled into the reactor.

Implementation

def async(*arguments, **options, &block)
	# warn "Async::Scheduler#async is deprecated. Use `run` or `Task#async` instead.", uplevel: 1, category: :deprecated
	
	Kernel.raise ClosedError if @selector.nil?
	
	task = Task.new(Task.current? || self, **options, &block)
	
	task.run(*arguments)
	
	return task
end

def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)

  • public
  • asynchronous

Invoke the block, but after the specified timeout, raise class Async::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.

Signature

public

Since Async v1.

asynchronous

May raise an exception at any interruption point (e.g. blocking operations).

parameter duration Numeric

The time in seconds, in which the task should complete.

parameter exception Class

The exception class to raise.

parameter message String

The message to pass to the exception.

yields {|duration| ...}

The block to execute with a timeout.

Implementation

def with_timeout(duration, exception = TimeoutError, message = "execution expired", &block)
	fiber = Fiber.current
	
	timer = @timers.after(duration) do
		if fiber.alive?
			fiber.raise(exception, message)
		end
	end
	
	yield timer
ensure
	timer&.cancel!
end

def timeout_after(duration, exception, message, &block)

  • public
  • asynchronous

Invoke the block, but after the specified timeout, raise the specified exception with the given message. 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.

Signature

public

Since Async v1 and Ruby v3.1. May be invoked from Timeout.timeout.

asynchronous

May raise an exception at any interruption point (e.g. blocking operations).

parameter duration Numeric

The time in seconds, in which the task should complete.

parameter exception Class

The exception class to raise.

parameter message String

The message to pass to the exception.

yields {|duration| ...}

The block to execute with a timeout.

Implementation

def timeout_after(duration, exception, message, &block)
	with_timeout(duration, exception, message) do |timer|
		yield duration
	end
end

Discussion