Re: Dumping input to NSTask
Re: Dumping input to NSTask
- Subject: Re: Dumping input to NSTask
- From: Shawn Erickson <email@hidden>
- Date: Wed, 14 Jul 2004 19:57:23 -0700
On Jul 14, 2004, at 7:15 PM, John Timmer wrote:
If you are not creating your own input and output pipes or file
handles, then that could be the problem. If you have created an
NSPipe instance to be used as the input, the write end is close when
the task is launched, so you need to send the data before you launch
the task.
Ah, that was definitely something I did wrong - thanks. Even so, I
wind up
with the task hanging. The following's the chunk of code that's
causing me
trouble - it stops right at the waitUntilExit indefinitely. If there's
anything else I'm obviously doing wrong, I'd appreciate hearing about
it.
Otherwise, I'll assume it's just some peculiarity with the executable
I'm
running and send things through a few files in /tmp.
NSTask *tempTask = [[NSTask alloc] init];
[tempTask setLaunchPath: theExecutablePath];
[tempTask setArguments: [NSArray arrayWithObject:
@"-format_output"]];
NSPipe *readPipe = [NSPipe pipe];
[tempTask setStandardOutput: readPipe];
NSFileHandle *readHandle = [readPipe fileHandleForReading];
NSPipe *writePipe = [NSPipe pipe];
[tempTask setStandardInput: writePipe];
NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
NSData *queryBytes = [NSData dataWithBytes: [theQuery UTF8String]
length: [theQuery length]];
[writeHandle writeData: queryBytes];
[tempTask launch];
[tempTask waitUntilExit]
If your process generates enough output to fill the pipe the process
will block waiting for the pipe to be drained. Pipes have finite
storage and once full things will block until space is made available
and this is done by read stuff out of the pipe.
So in the above you need to be draining the pipe and you can do that
using the background data notification methods provided by file
handles.
You should review the following code example...
<
http://developer.apple.com/samplecode/Moriarity/Moriarity.html>
In particular...
<
http://developer.apple.com/samplecode/Moriarity/listing5.html>
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.