From: Johan Andersen Date: 2006-04-24T01:40:39+09:00 Subject: Re: How to properly interact with processes? I just saw that the forum destroyed the code by wrapping the lines too early. Here is the code with shorter lines. def time(s) @st ||= Time.now printf "%s at: %fs\n", s, Time.now - @st end def run_proc(cmd,&callback) t = Thread.new(cmd){ IO.popen(cmd){|p| @pid = p.pid while(s=p.gets) callback.call(s) end } } while(!@pid) do sleep 0.01 end t end time("1. Starting external process") CMD = %{ruby -e "sleep 5;puts 'from process(#{$$})'"} t=run_proc(CMD) {|s| puts "4. Done! #{s}" } time("2. External process (PID:#{@pid}) started") time("3. Begun doing something else while proc works") t.join time("5. External proc done") -- Posted via http://www.ruby-forum.com/.