~singpolyma/em_fiberscheduler

8789f6781096fe2932497c8c0bbb85b52094ad9f — Stephen Paul Weber 2 months ago e279be1
Initial README
1 files changed, 34 insertions(+), 0 deletions(-)

A README.md
A README.md => README.md +34 -0
@@ 0,0 1,34 @@
# EM::FiberScheduler

This is a [fiber scheduler](https://docs.ruby-lang.org/en/3.1/Fiber/SchedulerInterface.html) for Ruby 3.0+ that uses EventMachine, allowing integration with projects that already use EventMachine.

If you are starting a new project and have no EventMachine-based dependencies, consider [fiber_scheduler](https://github.com/bruno-/fiber_scheduler) instead.

**NOTE:** fiber schedulers are per-thread settings.  This scheduler may **only** be used on the EventMachine reactor thread.

## Usage

    require "em_fiberscheduler"
    require "em-http"
    require "open-uri"

    Fiber.set_scheduler(EM::FiberScheduler.new)
	 
    Fiber.schedule do
      URI.open("https://httpbin.org/delay/2")
    end

    EM.schedule do
      EM::HttpRequest.new("https://httpbin.org/delay/2").get
    end

## Reactor Lifecycle

By default, `EM::FiberScheduler` will start the EventMachine reactor for you, and also stop it when all work is complete.  If any connection or server is still open, that will be considered work that is not yet complete.  If you want to disable the autostart do this:

    Fiber.set_scheduler(EM::FiberScheduler.new(autostart: false))

If you want to start EventMachine yourself but want the autostop behaviour, do this:

    Fiber.set_scheduler(EM::FiberScheduler.new(autostart: false))
    Fiber.scheduler.autostop