Sha256: c0515190d295a67d4c121452e86b30ac9a6ef3b5017681d9bb86a546dd71a1cb

Contents?: true

Size: 822 Bytes

Versions: 3

Compression:

Stored size: 822 Bytes

Contents

require 'nio'

module MultiProcess
  class Loop
    def initialize
      @selector = ::NIO::Selector.new

      Thread.new do
        loop do
          @selector.select(30.0) do |monitor|
            if monitor.io.eof?
              @selector.deregister(monitor.io)
              monitor.value.call(:eof, monitor)
            else
              monitor.value.call(:ready, monitor)
            end
          end

          # Wait very short time to allow scheduling another thread
          sleep(0.001)
        end
      end
    end

    def watch(io, &block)
      @selector.wakeup
      @selector.register(io, :r).tap do |monitor|
        monitor.value = block
        monitor.value.call(:registered, monitor)
      end
    end

    class << self
      def instance
        @instance ||= new
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
multi_process-1.1.1 lib/multi_process/loop.rb
multi_process-1.1.0 lib/multi_process/loop.rb
multi_process-1.0.0 lib/multi_process/loop.rb