[MacRuby-devel] MacRuby with Subprocesses
Matt Massicotte
massicotte at apple.com
Wed Nov 3 08:59:55 PDT 2010
Open3 works correctly for me. I've had tons of problems in the past using Open3 (in ruby) with interactive commands. I think the problem is that you are trying to run irb. The following works for me.
Open3.popen3('ls') { |stdin,stdout,stderr| puts stdout.readlines }
Backticks work as well, but are not nearly as powerful.
ls_output = `ls -l`
Matt
On Nov 3, 2010, at 7:31 AM, Louis-Philippe wrote:
> Hi all,
>
> I'm looking around MacRuby to find a way to run a subprocess and monitor it, here is what I tried:
>
> NSTask:
>
> framework "foundation"
> task = NSTask.launchedTaskWithLaunchPath("/bin/ls", arguments:"-l")
> p task.isRunning
> p task.standardOutput
>
> => Segmentation fault
>
> Open4:
>
> require 'rubygems'
> require 'popen4'
> status = POpen4::popen4('irb') { |stdout,stderr,stdin,pid|
> stdin.puts "a = 1"
> stdin.puts "a == 1"
> stdin.close
> puts "pid: #{pid}"
> puts "stdout: #{stdout.read.strip}"
> puts "stderr: #{stderr.read.strip}"
> }
> puts "status: #{status.inspect}"
> puts "exitstatus: #{status.exitstatus}"
>
> => /Library/Frameworks/MacRuby.framework/Versions/0.7/usr/lib/ruby/Gems/1.9.2/gems/open4-1.0.1/lib/open4.rb:15:in `open4:': fork() function is unimplemented on this machine (NotImplementedError)
> from /Library/Frameworks/MacRuby.framework/Versions/0.7/usr/lib/ruby/Gems/1.9.2/gems/POpen4-0.1.4/lib/popen4.rb:75:in `popen4:'
> from /Users/lpperron/Documents/lllaptop/git_repos/Redis/testResque/testOpen4.rb:3:in `<main>'
>
> Open3:
>
> require 'open3'
> puts "starting..."
> Open3.popen3('irb') { |stdin,stdout,stderr|
> stdin.puts "a = 1"
> stdin.puts "a == 1"
> stdin.close
>
> puts "stdout: #{stdout.read.strip}"
> puts "stderr: #{stderr.read.strip}"
> }
>
> => starting...
>
> So... NSTask segfaults... Open4 cannot work because of unimplemented 'fork' in MacRuby and Open3 hangs?
> Anybody has an other solution to launch and monitor a subprocess?
>
> Thanks!
>
> L-P
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
More information about the MacRuby-devel
mailing list