[MacRuby-devel] macruby NSTask problem

Colin McPhail mcphail_colin at hotmail.com
Tue Oct 11 02:27:33 PDT 2011


Alex Greif wrote ...
> I tried the subprocess sample with the AsyncHandler from
> http://ofps.oreilly.com/titles/9781449380373/_foundation.html
> and found the following problem:
> if I execute the code many times (I tried 20 times) in my terminal with
> $ macruby sample.rb
> then 2-3 times it does not print any output. In this 10% the data_ready:
> method of the AsyncHandler is not called.
> 
> Is this a macruby problem or an NSTask problem?

It may be that the order of delivery of the data_ready and task_terminated notifications is indeterminate.  I modified the task_terminated handler to set a 'terminated' flag rather than call exit.  At the bottom of the example I executed the run loop in 1-second slices until the terminated flag was set and then executed it for one more second in case the data_ready notification hadn't arrived yet.  Under light testing this seemed to give the expected result consistently.

Note that the example has another small defect: it issues only one readInBackgroundAndNotify call.  If the task produces a lot of output not all of it will necessarily be delivered by this single read.  The data_ready handler should really issue a further readInBackgroundAndNotify I think.

Regards,
-- CMcP


More information about the MacRuby-devel mailing list