Re: NSTask output and input - Related question
Re: NSTask output and input - Related question
- Subject: Re: NSTask output and input - Related question
- From: Will Mason <email@hidden>
- Date: Fri, 28 Jan 2005 10:07:32 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
If you call readInBackgroundAndNotify: on the NSFileHandle from which you would like to receive data, you'll get notified when data arrives, not just when the task is done. The trick, though, is that you need to call readInBackgroundAndNotify: every time that you receive a notification, or you will stop getting notified. In other words, claiming to be interested in the NSFileHandle's data is something you need to reassert continually.
By the way, April, it sounds like you might be only calling readInBackgroundAndNotify: once, as you're receiving partial data. Could not reasserting your interest be the problem you are experiencing?
Will
Rudi Sherry <email@hidden> wrote:
On Jan 20, 2005, at 10:33 AM, email@hidden wrote:
> This brings a point to mind.
> I have in several tasks I've interacted with, had the worst time
> trying to
> get the task output data. I set up the pipes exactly as prescribed in
> the
> link below and yet I get either no output from the task, or I get
> partial
> data. Is there a flaw in the NSTask or the NSPipe which causes it not
> to
> get all the data correctly?
> I've tried writing my own clu's to test it and the problem is worse.
> Whether I use printf and make sure to use a new line character at the
> end,
> or I use NSLog, fprintf(stdout,"text %i\n",variable); or what ever I
> tend
> to get absolutely no data, or I get only partial data.
>
> April.
I noticed that I only get the data!
when the
task is done (if anyone
knows how to get data while it's still executing, please let me know
what you use).
Here is the excerpt from my code:
NS_DURING
//NSLog( @"About to execute shell command:\r%@ %@\r", command, [
arguments componentsJoinedByString: @" " ] );
NSTask *task = [ [ NSTask alloc ] init ];
[ task setLaunchPath: command ];
[ task setArguments: arguments ? arguments : [ NSArray array ] ];
NSPipe *stdOutPipe = [ [[ NSPipe alloc ] init ] retain ];
[ task setStandardOutput: stdOutPipe ];
[ task setStandardError: stdOutPipe ];
NSFileHandle *stdOutFileHandle = [ stdOutPipe fileHandleForReading ];
// Do it. If we get an exception, we're through
BOOL ok = NO;
NS_DURING
[ task launch ];
ok = YES;
NS_HANDLER
NSLog( @"Error launching task:\r%@\r", [ localException description
] );
NS_ENDHANDLER
if ( ok == NO )
{
NSLog( @"EXCEPTION from [ task launch ]:\r%@\r%@", command,
arguments );
[ outputHandler outputDone ];
return @"";
}
unsigned keepGoing = (unsigned) YES;
while ( keepGoing == (unsigned) YES ) // TBD: timeout
{
// This raises a NSFileHandlerOperationException if the channel
isn't opened
// yet: let that happen a few times until the task launches.
NSData *avail = NULL;
keepGoing = [ task isRunning ];
if ( !keepGoing )
break;
NS_DURING
avail = [ stdOutFileHandle availableData ];
NS_HANDLER
//NSLog( @"found first exception; continuing\r" );
NS_ENDHANDLER
if ( avail != NULL && [ avail length ] == 0 )
{
[ outputHandler outputDone ];
//NSLog( @"Empty data returned: end of communication?" );
break;
}
else
{
NSString *availString = [ [ NSString alloc ] initWithData: avail
encoding: NSASCIIStringEncoding ];
//NSLog( @"Return in this loop:\r%@\r", availString );
[ outputHandler handleOutput: availString ];
stdOutString = [ stdOutString
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden