From: x1 Date: 2005-12-03T12:40:28+09:00 Subject: Re: Problem with method that starts process, yields pid then yields return code it seems to work!! Let me spend some time trying to make sense of it... Thanks soooooooooooooooo much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! *grins* On 12/2/05, ara.t.howard@noaa.gov wrote: > On Fri, 2 Dec 2005, x1 wrote: > > > I'm trying to create a method that will kick off a new process, return it's > > pid.. while allowing the return code to also be captured for later > > examination.. [on windows platform] > > should work, but untested on windows: > > harp:~ > cat a.rb > > require "thread" > class Exec > %w( cmd queue pipe pid thread exitstatus ).each{|a| attr_accessor a} > def initialize cmd > self.cmd = cmd > self.queue = ::Queue::new > self.thread = > ::Thread::new(queue) do |q| > pipe = ::IO::popen cmd > q.push pipe > q.push pipe.pid > begin > loop{ stdout = pipe.gets or break } > pipe.close > q.push $?.exitstatus > rescue => e > q.push e > end > end > self.pipe = queue.pop > self.pid = queue.pop > self.exitstatus = nil > end > def wait > self.exitstatus = self.queue.pop > end > def self::[](*a, &b) new *a, &b end > end > > require "yaml" > > y "start" => Time::now > sleep = Exec[ " ruby -e' sleep 2 and exit 42 ' " ] > y "cmd" => sleep.cmd > y "pid" => sleep.pid > y "exitstatus" => sleep.wait > y "finish" => Time::now > > harp:~ > ruby a.rb > --- > start: 2005-12-02 14:03:29.920649 -07:00 > --- > cmd: " ruby -e' sleep 2 and exit 42 ' " > --- > pid: 19947 > --- > exitstatus: 42 > --- > finish: 2005-12-02 14:03:31.930995 -07:00 > > > kind regards. > > -a > -- > =============================================================================== > | ara [dot] t [dot] howard [at] noaa [dot] gov > | all happiness comes from the desire for others to be happy. all misery > | comes from the desire for oneself to be happy. > | -- bodhicaryavatara > =============================================================================== > > >