Prototype two workers: blocking and multiplexing
This commit is contained in:
31
lib/uh/wm/workers/base.rb
Normal file
31
lib/uh/wm/workers/base.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
module Uh
|
||||
module WM
|
||||
module Workers
|
||||
class Base
|
||||
def initialize **options
|
||||
@ios = []
|
||||
end
|
||||
|
||||
def watch io
|
||||
@ios << io
|
||||
end
|
||||
|
||||
def before_wait &block
|
||||
if block_given? then @before_wait = block else @before_wait end
|
||||
end
|
||||
|
||||
def on_timeout &block
|
||||
if block_given? then @on_timeout = block else @on_timeout end
|
||||
end
|
||||
|
||||
def on_read &block
|
||||
if block_given? then @on_read = block else @on_read end
|
||||
end
|
||||
|
||||
def on_read_next &block
|
||||
if block_given? then @on_read_next = block else @on_read_next end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/uh/wm/workers/blocking.rb
Normal file
15
lib/uh/wm/workers/blocking.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
module Uh
|
||||
module WM
|
||||
module Workers
|
||||
class Blocking < Base
|
||||
def work_events
|
||||
#until yield
|
||||
# @on_events_read_bang.call
|
||||
#end
|
||||
#@on_events_read_bang.call until yield
|
||||
@on_read_next.call
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
18
lib/uh/wm/workers/mux.rb
Normal file
18
lib/uh/wm/workers/mux.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Uh
|
||||
module WM
|
||||
module Workers
|
||||
class Mux < Base
|
||||
def initialize timeout: 1
|
||||
super
|
||||
@timeout = timeout
|
||||
end
|
||||
|
||||
def work_events
|
||||
@before_wait.call if @before_wait
|
||||
if res = select(@ios, [], [], @timeout) then @on_read.call res
|
||||
else @on_timeout.call if @on_timeout end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user