Skip to content
On this page

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class · Ruby 4.1

Fiber::Scheduler

< Object 18 instance methods

This is not an existing class, but documentation of the interface that Scheduler object should comply to in order to be used as argument to Fiber.scheduler and handle non-blocking fibers. See also the “Non-blocking fibers” section in Fiber class docs for explanations of some concepts.

Scheduler’s behavior and usage are expected to be as follows:

  • When the execution in the non-blocking Fiber reaches some blocking operation (like sleep, wait for a process, or a non-ready I/O), it calls some of the scheduler’s hook methods, listed below.

  • Scheduler somehow registers what the current fiber is waiting on, and yields control to other fibers with Fiber.yield (so the fiber would be suspended while expecting its wait to end, and other fibers in the same thread can perform)

  • At the end of the current thread execution, the scheduler’s method scheduler_close is called

  • The scheduler runs into a wait loop, checking all the blocked fibers (which it has registered on hook calls) and resuming them when the awaited resource is ready (e.g. I/O ready or sleep time elapsed).

This way concurrent execution will be achieved transparently for every individual Fiber’s code.

Scheduler implementations are provided by gems, like Async.

Hook methods are:

The block, unblock, kernel_sleep, io_wait, and fiber_interrupt hooks are mandatory. Other hooks are optional unless specified otherwise.

It is also strongly recommended that the scheduler implements the fiber method, which is delegated to by Fiber.schedule.

Sample toy implementation of the scheduler can be found in Ruby’s code, in test/fiber/scheduler.rb

Instance methods 18

Ruby version changes 7

  1. 4.1
    • The fiber_interrupt hook is now required. Schedulers which do not implement it can no longer be installed with Fiber.set_scheduler.

  2. 4.0
  3. 3.4
  4. 3.2