Getting pipe output before command returns

J

jvivenot

Hi,

In a Gnome Ruby application, I want to display the output of a bash
command during its own execution, instead of after it finished.
In order to do this, I used :
io = IO::popen(...)
io.each_line {

}

But this displays the output after the command returned. How could I do
? (my command takes several minutes, and displays things every two
seconds, that's why I can't wait...)

Thanks.
(I hope my english is understandable.)
 
B

Bernhard 'elven' Stoeckner

jvivenot scribbled on Wednesday 24 May 2006 17:37, fup2
Hi,

In a Gnome Ruby application, I want to display the output of a bash
command during its own execution, instead of after it finished.
In order to do this, I used :
io = IO::popen(...)
io.each_line {

}

But this displays the output after the command returned. How could I do
? (my command takes several minutes, and displays things every two
seconds, that's why I can't wait...)

Thanks.
(I hope my english is understandable.)

IO#gets() does what you want to achieve. :)
 
R

Robert Klemme

Bernhard said:
jvivenot scribbled on Wednesday 24 May 2006 17:37, fup2


IO#gets() does what you want to achieve. :)

I doubt this will make a difference as it's just another interface to
the same functionality. Possible reasons why the output does not show up:

- the command does not send line oriented output

- the command uses output buffering of its own

- the ruby script uses output buffering

- the part of the ruby script that iterates through the output read
from the pipe blocks UI activity (Gnome!).

To work around the first issue you can use fixed size buffer reads:

IO.popen("bash -c 'ls -lR'") do |io|
while ( buffer = io.read(1024) )
stdout.write buffer
end
end

For the last one (interference with UI toolkit) I don't have a solution
- maybe putting the popen call into a thread helps.

Kind regards

robert
 

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,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top