I have a client.rb
program which prints a few lines of text and then pauses for user input. I'm trying to wrap this using PTY.spawn
(or another subprocess library, if the functionality's not available here).
Using stdout.readline
I can print the few lines of output. The problem is, once the output's done (and it's waiting for my input), stdout.readline
just hangs.
require 'pty'
PTY.spawn("ruby client.rb") do |stdout, stdin, thread|
until (line=stdout.readline).empty?; puts line; end
end
I am looking for a method on stdout
that will return nil
or ''
if there's nothing there. I suppose I could use rescue
and Timeout
but I'm hoping there's a better way.
I should note that I've had the same issue using other methods to read from stdout
: each_line
, each
, read
, readlines
. In all these cases, it hangs for > 30 seconds then raises:
Errno::EIO Exception: Input/output error @ io_fillbuf
After this error is raised, trying to read from stdout
again causes the same error to be raised, but now it happens immediately.
I've seen in various tutorials (i.e. this answer) that the Errno::EIO
can be rescued. However it's not working for me. Even if I command client.rb
to print more output, trying to read from stdout
always shows this error once it's been raised the first time.