Skip to content

Instantly share code, notes, and snippets.

@rickhull
Created June 14, 2013 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickhull/5778575 to your computer and use it in GitHub Desktop.
Save rickhull/5778575 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'thread'
reader, writer = IO.pipe
t = Thread.new {
loop {
duration = rand(5) + 1
sleep(duration)
writer.write "slept for #{duration} seconds"
}
}
puts "starting the select loop -- ctrl-c to quit"
puts "monitoring one side of an IO pipe, as well as STDIN"
puts "try typing in some input for STDIN..."
loop {
# blocks until there is something to process
#
read_ios, write_ios, error_ios = select([reader, $stdin])
read_ios.each { |ri|
puts "#{ri} has something to read"
puts "readpartial(255): #{ri.readpartial(255)}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment