On Fri, 13 May 2005, Lawrence Oluyede wrote:

> Ara.T.Howard / noaa.gov writes:
>
>> send the data down in a thread
>>
>>    data = IO::read 'huge_file'
>>    sender = Thread::new(data){data.each{|line| pipe.puts line}}
>>
>> you'd probably then want a reader to load a thread safe queue with the
>> returned data.
>
> That's what I was doing before with popen3, wrap the function with a Thread.
> Let me try again
>
>> why don't you want to block?
>
> Cause it hangs the UI (a webpage)

ah, why didn't you say so? ;-)  that's __exactly__ why i designed my session
lib - to NOT hang tk apps.  it's thread safe so you can do this:

   class UI
     def initialize
       @session = Session::new
       @stdout_widget = SomeWidget::new
       @stderr_widget = SomeWidget::new
     end

     def button_pressed
       command = get_system_command

       Thread::new(session, command) do |s, c|
         s.execute(c) do |stdout, stderr|
           @stdout_widget.update stdout if stdout
           @stderr_widget.update stderr if stderr
         end
       end
     end
   end

and this runs in the background.  the block for stdout and stderr are handled
as output is produced.  session runs commands in the shell and so you don't
have a handle on the stdin in the process, but this would be easy to solve via

   require 'tempfile'

   def run_background_command command, input
     tmp = Tempfile::new rand.to_s
     begin
       tmp.write input
       tmp.close

       command = "#{ command } < #{ tmp.path }"

       Thread::new do
         @session.execute(command) do |stdout, stderr|
           async_handle stdout if stdout
           async_handle stderr if stderr
         end
       end
     ensure
       tmp.close! if tmp
     end
   end
   ...
   ...
   dont_forget_to_join_this_thread = run_background_command 'ui_hanger.exe', 'abc'
   ...
   ...
   dont_forget_to_join_this_thread.join

make sense?

   http://www.codeforpeople.com/lib/ruby/session/
   http://raa.ruby-lang.org/project/session/

hth.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================