On Sunday 01 June 2008, ara.t.howard wrote:
> On May 31, 2008, at 10:16 AM, Stefano Crocco wrote:
> > I don't think that would work. If I understand correctly what you
> > mean, that
> > should be put in the script which produces the output, not not in
> > the one
> > displaying it. But since the output I want to display is from a
> > script written
> > by the user, I can't do that.
>
> what you want to do is easily accomplished using session.rb
>
> http://codeforpeople.com/lib/ruby/session/session-2.4.0/README
>
> gem install session
>
>
> note that i wrote to do *exactly* what you are doing - but it was for
> a tk gui.  anyhow, you *must* do this in a background thread as there
> are certain things child programs can do (like setvbuf) which can
> defeat any counter buffering techniques you may try - so you must
> always process the stdout/stderr asynchronously in a background thread
> which updates the gui.  session makes this trivial to accomplish.
>
> kind regards.
>
> a @ http://codeforpeople.com/
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama

Thanks for telling me about session. Unfortunately, it still doesn't work. I 
tried with the following code (there's no ui because I wanted to understand 
how session works before using it in a more complex situation):

require 'session'

t=Thread.new do
  sh = Session.new
  sh.execute( 'ruby /home/stefano/documenti/scripts/prova.rb' ) do |out, err|
    puts "Msg: #{out}" if out
    puts "Err: #{err}" if err
  end
end
t.join

The result is the same I already got, that is: all the error messages together 
and all the stdout messages together. At any rate, tried modifiying my test 
script:

$stdout.sync=true #added line
10.times{|i|
  puts i
  warn "Possible error #{i}"
  sleep 1 #added line
}

With the introduction of the two lines marked with 'added line', the output is 
the one expected (even if it seems that the order in wich the error message 
and the stdout message are displayed for each iteration is random). Removing 
one of those lines brings it back to the 'wrong' behavior. 

However, I've tried to run the same script using other editors
(kdevelop, scite, netbeans) and the results are the same as mine. This seems 
to mean that there's not an easy way to accomplish what I want to do.