Re: NSTask interactive question
Re: NSTask interactive question
- Subject: Re: NSTask interactive question
- From: "Shawn Erickson" <email@hidden>
- Date: Wed, 29 Nov 2006 12:17:23 -0800
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:
This email sent to email@hidden