[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Getting pipe output before command returns

jvivenot

5/24/2006 3:38:00 PM

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.)

3 Answers

Bernhard 'elven' Stoeckner

5/24/2006 3:41:00 PM

0

jvivenot scribbled on Wednesday 24 May 2006 17:37, fup2
<1148485072.662376.323810@j55g2000cwa.googlegroups.com>:

> 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. :)

jvivenot

5/24/2006 3:42:00 PM

0

That's OK. Many thanks !

Robert Klemme

5/24/2006 5:18:00 PM

0

Bernhard 'elven' Stoeckner wrote:
> jvivenot scribbled on Wednesday 24 May 2006 17:37, fup2
> <1148485072.662376.323810@j55g2000cwa.googlegroups.com>:
>
>> 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. :)

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