[MacRuby-devel] MacRuby with Subprocesses

Louis-Philippe default at spiralix.org
Wed Nov 3 09:59:33 PDT 2010


Great!  Thanks all for the answers!

I would have really liked to use backticks or open3, as they are really
easier to use, but what my example didn't show is that my subprocess will be
interactive, so in that case NSTask really seems like the way to go.

For NSTask segfault, you were right, it was only because arguments were
supposed to be an array, now it works!

Feels good to be able to use NSTask from macruby, lots of power in there!


2010/11/3 Matt Aimonetti <mattaimonetti at gmail.com>

> I have to respectfully agree that open3 is easier to use, however it
> doesn't offer some of the more advanced features offered by NSTask.
>
> I also found out why you are seeing a segfault, the arguments should be
> passed in an array, try:
>
>
> framework "foundation"
> task = NSTask.launchedTaskWithLaunchPath("/bin/ls", arguments:["-l"])
> p task.isRunning
> p task.standardOutput
>
> # keep the run loop running
> NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture)
>
> This code works for me.
>
> - Matt
>
>
>
> On Wed, Nov 3, 2010 at 9:12 AM, Matt Massicotte <massicotte at apple.com>wrote:
>
>> I respectfully disagree :)
>>
>> NSTask is a huge pain and will result in dramatically more code in the
>> simple cases.  Open3 is much easier to work with, and we can only dream of a
>> solution as simple as backticks in Cocoa.  It becomes less of an issue if
>> you are executing long-running processes and do not want to block on their
>> completion.
>>
>> All that said, using the Cocoa implementations is definitely generally
>> preferred.  But, if Ruby offers something that Cocoa either doesn't or
>> doesn't do well, you should:
>>
>> - use the one that best fits your problem
>> - file a bug so the Cocoa version can be improved
>>
>> Matt
>>
>> On Nov 3, 2010, at 9:00 AM, Matt Aimonetti wrote:
>>
>> > NSTask is the way to go, I used it in many cases, including some
>> examples in my O'Reilly book:
>> > http://macruby.labs.oreilly.com/ch04.html#_tasks_subprocesses
>> >
>> > You can also look at this more complex example:
>> >
>> >
>> https://github.com/mattetti/couchdbx-app/tree/master/macruby_version/CouchDBX/
>> >
>> > The wrappers might be interesting to look at to see how I'm hiding some
>> of the complexity.
>> >
>> > - Matt
>> >
>> > Sent from my iPhone
>> >
>> > On Nov 3, 2010, at 7:31, Louis-Philippe <default at spiralix.org> 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
>> > _______________________________________________
>> > MacRuby-devel mailing list
>> > MacRuby-devel at lists.macosforge.org
>> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20101103/563584e9/attachment.html>


More information about the MacRuby-devel mailing list