Re: Reading data from an NSTask
Re: Reading data from an NSTask
- Subject: Re: Reading data from an NSTask
- From: Ken Tozier <email@hidden>
- Date: Mon, 4 Jul 2005 17:41:41 -0400
On Jul 4, 2005, at 4:47 PM, j o a r wrote:
Right, and if you DON'T set up the output pipes, it will probably
work just fine - try it out!
That's closer. If I comment out
//[testTask setStandardOutput: [NSPipe pipe]];
//[testTask setStandardError: [NSPipe pipe]];
The results print to the run log but that print isn't caused by any
of my NSLogs, it happens automagically. An earlier response from "
Pontus Ilbring" seemed to indicate that, if you want to get ahold of
the data, it is -mandatory- to initialize setStandardOutput with
your own [NSPipe pipe], otherwise, the environment defaults to the
app running the task and it's data gets consumed/destroyed by the act
of writing it to the run log.
If I step through in the debugger, it chokes at the "NSData *taskData
= [[testTask standardOutput] readDataToEndOfFile];" in this segment
if ([testTask terminationStatus] == 0)
{
// success
NSData *taskData = [[testTask
standardOutput] readDataToEndOfFile];
NSLog(@"taskData = %@", taskData);
NSString *taskDataString = [[NSString alloc]
initWithData: taskData encoding: NSUnicodeStringEncoding];
NSLog(@"taskDataString = %@", taskDataString);
}
Better than it was, but it seems Pontus's explanation of the task
being consumed by my app environment before it gets to the code that
tries to extract it from standardOutput, is correct.
I may have to go the asynchronous route, but it seems like I'm
just making a minor error here. It should work. Any glaring errors
jump out at anyone?
Just the one I've mentioned from the start:
"Your program can communicate with the task by attaching one or
more NSPipe instances to the task’s standard input, output, or
error devices before launching the task.
I interpret this to mean that attaching a pipe is -mandatory- if you
ever want to use the results
[testTask setStandardOutput: [NSPipe pipe]];
that's also the way it was done in your first link <http://
www.cocoabuilder.com/archive/message/cocoa/2003/12/29/78363>
[perlControl setStandardOutput:[NSPipe pipe]];
[perlControl setLaunchPath:pathToXJcc];
[perlControl setArguments:args];
[perlControl launch];
[perlControl waitUntilExit];
He seems to be doing exactly what I'm doing, no registering for
callbacks, no asynchronous handlers etc but mine doesn't work.
I appreciate your help, but this topic isn't completely unambiguous.
perhaps a snippet would clear things up?
A pipe is a one-way communications channel between related
processes; one process writes data while the other process reads
that data. The data that passes through the pipe is buffered; the
size of the buffer is determined by the underlying operating system."
I guess I don't get where "the buffer" fits in here. NSPipe doesn't
have a method for using your own buffer. It provides a fileHandle and
file handles have a readToEndOfFile method. Nowhere does it seem that
NSPipes can -only- be used asynchronously.
Once you try to funnel the output of your task into the pipe,
you're subject to the limitations of that pipe.
I'm not trying to put anything into the pipe, I'm trying to read
something out of it. Are the terms bass-ackwards? "setStandardOutput"
is where the launching code puts data and "setStandardInput" is where
it fetches data? Rather than the reverse (which is how I'm
interpreting "input" and "output".
Thanks again
Ken
_______________________________________________
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