Class: MultiProcess::Loop
- Inherits:
-
Object
- Object
- MultiProcess::Loop
- Defined in:
- lib/multi_process/loop.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Loop
constructor
A new instance of Loop.
- #watch(io, &block) ⇒ Object
Constructor Details
#initialize ⇒ Loop
Returns a new instance of Loop.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/multi_process/loop.rb', line 5 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
.instance ⇒ Object
34 35 36 |
# File 'lib/multi_process/loop.rb', line 34 def instance @instance ||= new end |
Instance Method Details
#watch(io, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/multi_process/loop.rb', line 25 def watch(io, &block) @selector.wakeup @selector.register(io, :r).tap do |monitor| monitor.value = block monitor.value.call(:registered, monitor) end end |