Capturing incremental output from STDOUT

J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

Hi all,

If I run an external process, e.g.

IO.popen('cucumber')

Then Ruby blocks while waiting for the whole output of the subprocess. Is
there a way to read the subprocess' output incrementally as it is being
written?
 
B

Brian Candler

James said:
Hi all,

If I run an external process, e.g.

IO.popen('cucumber')

Then Ruby blocks while waiting for the whole output of the subprocess.
Is
there a way to read the subprocess' output incrementally as it is being
written?

What's almost certainly happening is that the process which is sending
the output is buffering it, and not flushing the buffer.

If the subprocess is written in ruby, try adding "$stdout.sync = true"
at the beginning.

Some apps will switch into unbuffered mode if they think they are
talking to a human on a terminal (a tty or pty). There is an
almost-undocumented 'pty' module in the standard library which you can
use to run a program under a pty: see

http://www.ruby-forum.com/topic/133560

so give that a try if the process you're spawning is not one that you
can modify.
 
R

Robert Klemme

2010/3/8 James Coglan said:
If I run an external process, e.g.

IO.popen('cucumber')

Then Ruby blocks while waiting for the whole output of the subprocess.

IO.popen does not block anything. You would at least have to read
from or write to the stream in order to get a chance of blocking.
Is
there a way to read the subprocess' output incrementally as it is being
written?

You can use IO#read(int), i.e. with a size limit, or you can read line
based. What code do you have and what do you want to accomplish?

Kind regards

robert
 
B

Brian Candler

Robert said:
IO.popen does not block anything. You would at least have to read
from or write to the stream in order to get a chance of blocking.

Although the OP didn't mention the platform, as Windows can be broken
with regards to forking.

But I just tried it with the one-click installer under XP, and it seems
to be fine:

ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]
EOFError: end of file reached

(readpartial reads between 1 and the given number of bytes, depending on
how many are available at the time)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top