From: ara.t.howard@... Date: 2005-12-04T08:59:53+09:00 Subject: Re: Problem with method that starts process, yields pid then yields return code 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 ===============================================================================