Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Capturing incremental output from STDOUT

Reply
Thread Tools

Capturing incremental output from STDOUT

 
 
James Coglan
Guest
Posts: n/a
 
      03-08-2010
[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?


--
James Coglan
http://jcoglan.com
+44 (0) 7771512510

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      03-10-2010
James Coglan wrote:
> 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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-10-2010
2010/3/8 James Coglan <>:

> 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


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      03-10-2010
Robert Klemme wrote:
> 2010/3/8 James Coglan <>:
>
>> 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.


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]

>> c = IO.popen("pause","w+")

=> #<IO:0x2bea818>
>> c.readpartial(1024)

=> "Press any key to continue . . . "
>> c.puts

=> nil
>> c.readpartial(1024)

=> "\n"
>> c.readpartial(1024)

EOFError: end of file reached

(readpartial reads between 1 and the given number of bytes, depending on
how many are available at the time)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing stdout from a class method Falcolas Python 1 06-27-2007 07:48 PM
Capturing stderr and stdout of a subprocess as a single stream Fuzzyman Python 3 01-07-2007 08:44 PM
Capturing stdout without waiting for the process end Luigi Python 5 04-03-2006 07:24 PM
capturing stdout from lynx.. sergio@village-buzz.com Python 2 03-13-2006 03:07 PM
Capturing stdout incrementally Moosebumps Python 5 04-07-2004 03:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57