Re: NSTask problems.
Re: NSTask problems.
- Subject: Re: NSTask problems.
- From: Ingvar Nedrebo <email@hidden>
- Date: Thu, 25 Jul 2002 14:16:57 +0100
Others have already suggested CURLHandle from SourceForge, but see below
for what the porblem is with your approach.
On Wednesday, July 24, 2002, at 10:06 , Olivier wrote:
- (IBAction)loadURL:(id)sender
{
NSTask *cURLSession = [[NSTask alloc] init];
NSPipe *shellOutput =[[NSPipe alloc] init];
NSFileHandle *fileHandle;
NSString *result;
[cURLSession setLaunchPath:@"/usr/bin/curl"];
[cURLSession setArguments:[NSArray
arrayWithObjects:@"http://ambush.avara.net/index.php",nil]];
[cURLSession setStandardOutput:shellOutput];
[cURLSession setStandardError:shellOutput];
fileHandle = [shellOutput fileHandleForReading];
[cURLSession launch];
while([cURLSession isRunning]) // While the proc. is running.
{
// Do nothing and wait till it's finished...
}
This is what is know as a "busy wait", and although not wrong, is not a
good approach because you're wasting CPU cycles. What you want is
"-waitUntilExit" (see NSTask doc).
NSLog(@"Termination Status: %d",[cURLSession terminationStatus]);
result=[[NSString alloc] initWithData:[fileHandle
readDataToEndOfFile]
encoding:NSASCIIStringEncoding];
Here is your problem. You are not reading any data from the pipe until
after the task has terminated.
- the task will start writing to the pipe while your app is waiting for
it to finish.
- if the output from the task is less than the pipe's buffer size, the
task will finish and you will get the data from the fileHandle.
- if the output from the task fills up the buffer, the task will block
waiting for the buffer to empty.
- your app is waiting for the task to finish, so it will never read from
the pipe and thus never empty the buffer, and you have a classic
deadlock situation: app waiting for task, task waiting for app.
See
<
http://developer.apple.com/samplecode/Sample_Code/Cocoa/Moriarity.htm>
for one possible solution: asynchronous background reading. There are
other possible ways to skin this particulat cat, such as the synchronous
way of replacing your busy-wait loop with a read loop that reads chunks
of data from the pipe until a length of zero is returned, which
indicates end-of-file.
I.
_______________________________________________
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.