ruby-changes:4704
From: ko1@a...
Date: Sat, 26 Apr 2008 13:04:15 +0900 (JST)
Subject: [ruby-changes:4704] akr - Ruby:r16198 (trunk): * lib/open3.rb: double fork is replaced by spawn with Process.detach.
akr 2008-04-26 13:03:59 +0900 (Sat, 26 Apr 2008) New Revision: 16198 Modified files: trunk/ChangeLog trunk/lib/open3.rb Log: * lib/open3.rb: double fork is replaced by spawn with Process.detach. (Open3.popen3w): new method to access the thread returned by Process.detach. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16198&r2=16197&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/open3.rb?r1=16198&r2=16197&diff_format=u Index: ChangeLog =================================================================== --- ChangeLog (revision 16197) +++ ChangeLog (revision 16198) @@ -1,3 +1,9 @@ +Sat Apr 26 13:00:41 2008 Tanaka Akira <akr@f...> + + * lib/open3.rb: double fork is replaced by spawn with Process.detach. + (Open3.popen3w): new method to access the thread returned by + Process.detach. + Sat Apr 26 00:47:43 2008 Tanaka Akira <akr@f...> * process.c (rb_spawn_internal): new function to specify Index: lib/open3.rb =================================================================== --- lib/open3.rb (revision 16197) +++ lib/open3.rb (revision 16198) @@ -17,6 +17,8 @@ # # stdin, stdout, stderr = popen3('nroff -man') # +# If the exit status of the child process is required, Open3.popen3w is usable. +# # Open3.popen3 can also take a block which will receive stdin, stdout and # stderr as parameters. This ensures stdin, stdout and stderr are closed # once the block exits. Example: @@ -29,62 +31,79 @@ module Open3 # # Open stdin, stdout, and stderr streams and start external executable. + # # Non-block form: # - # require 'open3' + # stdin, stdout, stderr = Open3.popen3(cmd) + # ... + # stdin.close # stdin, stdout and stderr should be closed in this form. + # stdout.close + # stderr.close # - # [stdin, stdout, stderr] = Open3.popen3(cmd) - # # Block form: # # require 'open3' # # Open3.popen3(cmd) { |stdin, stdout, stderr| ... } + # # stdin, stdout and stderr is closed automatically in this form. # - # The parameter +cmd+ is passed directly to Kernel#exec. + # The parameter +cmd+ is passed directly to Kernel#spawn. # def popen3(*cmd) + if defined? yield + popen3w(*cmd) {|stdin, stdout, stderr, wait_thr| + yield stdin, stdout, stderr + } + else + stdin, stdout, stderr, wait_thr = popen3w(*cmd) + return stdin, stdout, stderr + end + end + module_function :popen3 + + # + # Open stdin, stdout, and stderr streams and start external executable. + # In addition, a thread for waiting the started process is noticed. + # + # Non-block form: + # + # stdin, stdout, stderr, wait_thr = Open3.popen3w(cmd) + # ... + # stdin.close # stdin, stdout and stderr should be closed in this form. + # stdout.close + # stderr.close + # exit_status = wait_thr.value # Process::Status object returned. + # + # Block form: + # + # Open3.popen3w(cmd) { |stdin, stdout, stderr, wait_thr| ... } + # + # The parameter +cmd+ is passed directly to Kernel#spawn. + # + def popen3w(*cmd) pw = IO::pipe # pipe[0] for read, pipe[1] for write pr = IO::pipe pe = IO::pipe - pid = fork{ - # child - fork{ - # grandchild - pw[1].close - STDIN.reopen(pw[0]) - pw[0].close - - pr[0].close - STDOUT.reopen(pr[1]) - pr[1].close - - pe[0].close - STDERR.reopen(pe[1]) - pe[1].close - - exec(*cmd) - } - exit!(0) - } - + pid = spawn(*cmd, STDIN=>pw[0], STDOUT=>pr[1], STDERR=>pe[1]) + wait_thr = Process.detach(pid) + wait_thr[:pid] = pid pw[0].close pr[1].close pe[1].close - Process.waitpid(pid) - pi = [pw[1], pr[0], pe[0]] + pi = [pw[1], pr[0], pe[0], wait_thr] pw[1].sync = true if defined? yield begin return yield(*pi) ensure - pi.each{|p| p.close unless p.closed?} + [pw[1], pr[0], pe[0]].each{|p| p.close unless p.closed?} + wait_thr.join end end pi end - module_function :popen3 + module_function :popen3w end if $0 == __FILE__ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/