From: x1 Date: 2005-12-06T06:42:31+09:00 Subject: Re: Problem with method that starts process, yields pid then yields return code What we have so far works, however ideally, printing the pid should not rely on the completion of the job. The only thing that should rely on the completion of the job would be outputting the exitstatus. Here's my vision.. class JobServer def execute(cmd) # kicks off the job and returns the pid return pid end def status(pid) # returns the status of the job. IE is the pid alive? or dead? return pid.alive? end def exitstatus(pid) # returns the exitstatus based on the PID return pid.exitstatus end end # Scenerio: I need to kick off two jobs and a third upon completion of the first (when exit statis is 0). # Ideally, one would be able to kick off a job with execute() and know about it's pid. Then later on, be able to reference the status of that pid, such as alive? or dead?. If dead, one could call the exitstatus() method to find out what the return code of that job was. case mode when %r/server/i DRb.start_service "druby://#{ hostname }:#{ port }", JobServer.new DRb.thread.join when %r/client/i DRb.start_service job = DRbObject.new(nil, "druby://#{ hostname }:#{ port }") pid = job.execute("ruby -e' sleep 3 and exit 43 '") # without having to wait on the execution above to finish: if job.status(pid) == true puts "exit status = " + job.existstatus(pid) else puts "job still running" end end #Again, this code isnt functional, just a concept of how I would see. What's preventing me from turning waht we have into this is my lack of knowledge in the following w/ your code: # 1) not exactly sure attr_accessor does # 2) not sure what Queue.new does # 3) not sure what def initialize does # Again, I really� appreciate your help in understanding and assistance. On 12/3/05, ara.t.howard@noaa.gov wrote: > On Sun, 4 Dec 2005, x1 wrote: > > > I've hacked at it and managed to learn quite a few things in the > > process but I cant seem to assign the Exec class to a DRb server to > > handle requests. > > > > Ideally, the would be able to do something like: > > > > harp:~ > cat servant.rb > #! /usr/bin/env ruby > %w( thread drb socket time yaml ).each{|lib| require lib} > > class Execute > %w( cmd queue pipe pid thread exitstatus alive ).each{|a| attr_accessor a} > def initialize(cmd) > @cmd = cmd > @queue = Queue.new > @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 > @pipe = queue.pop > @pid = queue.pop > @exitstatus = nil > end > def wait > @exitstatus = @queue.pop > end > end > > class Executioner > def execute cmd > Execute::new cmd > end > end > > mode = ARGV.shift > hostname = Socket.gethostname > port = 4501 > > case mode > when %r/server/i > DRb.start_service "druby://#{ hostname }:#{ port }", Executioner.new > DRb.thread.join > > when %r/client/i > DRb.start_service > executioner = DRbObject.new nil, "druby://#{ hostname }:#{ port }" > sleep = executioner.execute "ruby -e' sleep 3 and exit 43 '" > y "start" => Time::now.iso8601 > y "pid" => sleep.pid > y "exitstatus" => sleep.wait > y "finish" => Time::now.iso8601 > end > > > > harp:~ > ./servant.rb server & > [1] 15727 > > > > harp:~ > ./servant.rb client > --- > start: "2005-12-03T16:58:19-07:00" > --- > pid: 15729 > --- > exitstatus: 43 > --- > finish: "2005-12-03T16:58:22-07:00" > > > hth. > > > -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 > =============================================================================== > > >