Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask interactive question



On 11/29/06, Matt Neuburg <email@hidden> wrote:
I have an NSTask which is a Ruby interactive script. The Ruby script just
loops on standard input, like this:

while (s = gets)
  puts gk.crunch(s) # never mind what that does
  $stdout.flush
end

I'm launching this NSTask and then I'm repeatedly sending input at it and
retrieving the output, all in a single routine, without using notifications,
like this:

NSString* input = [NSString stringWithFormat: @"%@\n", s];
NSFileHandle* fh = [[task standardInput] fileHandleForWriting];
[fh writeData:[input dataUsingEncoding: NSMacOSRomanStringEncoding]];
NSFileHandle* fh2 = [[task standardOutput] fileHandleForReading];
NSString* output = [[[NSString alloc] initWithData: [fh2 availableData]
   encoding: NSUTF8StringEncoding] autorelease];

I'm just doing that over and over again, supplying input and retrieving
output in a single step. It's working fine. My question is: even though it's
working, am I in fact cruising for a bruising? Must I instead use the more
complicated readInBackgroundAndNotify architecture? m.

Nothing in the above is ensuring that you get a complete response from the task since availableData may be a subset of what the task has so far been able to generate after you send out the input. Also nothing ensures that you might not flood the task with input so that the output you read is unrelated to what you just sent down.

You don't need to use read in background but you do need to ensure you
are in sync with your task in some fashion (note read in background
wouldn't solve this issue for you either).

-Shawn
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >NSTask interactive question (From: Matt Neuburg <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.