Class: MultiProcess::Loop

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_process/loop.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoop

Returns a new instance of Loop.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/multi_process/loop.rb', line 7

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

Class Method Details

.instanceObject



36
37
38
# File 'lib/multi_process/loop.rb', line 36

def instance
  @instance ||= new
end

Instance Method Details

#watch(io, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/multi_process/loop.rb', line 27

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