Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save csmr/2bbf4ee73a3860c9bf44c2c65dad4e96 to your computer and use it in GitHub Desktop.
Save csmr/2bbf4ee73a3860c9bf44c2c65dad4e96 to your computer and use it in GitHub Desktop.
Example nonblocking read from a Named Pipe (created elsewhere)
class Engine
def initialize
#...
# elsewhere a named pipe has been created in filesys using:
# system("mkfifo #{fpath}") unless File.exists? fpath
# call named_pipe method which wraps IO.open( fpath, flag )
@pipe_from_ui = named_pipe( Pipes[:engine_in], "r" )
msg_ui = @pipe_from_ui.gets # blocks until ui writes in there:w
#...
end
def main_loop
# ...
while _exec_state_int == 1
if _tns <= Time.now.to_i
p "***** #{__FILE__}: - #{Time.now} thread: #{Thread.current.to_s} - L/MIN: #{_loops}"
_tns = Time.now.to_i + 60
_loops = 0
end
# dummy_work(10) # delay in milliseconds
# handle messages
msg_ui = nil
rwe = IO.select([@pipe_from_ui], [], [], 0)
msg_ui = rwe[0][0].read(2) unless rwe == nil
if msg_ui != nil
msg_ui = msg_ui.strip
p " ***** Engine got ui message: #{msg_ui}"
# do message related stuff
end
_loops+=1
end
end
# other stuff...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment