Use NSTask To Run SH Script in Objective C slower than direct execute script from Terminator

150 Views Asked by At

I created a sh/Python script as execute file. However, when I run this script by one click button from Objective C program using NSTask the progress time is much slower than I execute script directly from MacOS Terminal.

I read some information from stackoverlfow to use nice -n but it doesn't help. Any one has idea how to speed up process when execute script through Objective C.

Code To Execute script:

- (NSString *)runCommand:(NSString*) script{
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];

NSArray *arguments = [NSArray arrayWithObjects:
                      @"-c" ,
                      [NSString stringWithFormat:@"%@", script],
                      nil];
[task setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

NSPipe* stdErrPipe = nil;
stdErrPipe = [NSPipe pipe];
[task setStandardError: stdErrPipe];

NSFileHandle *file = [pipe fileHandleForReading];
NSFileHandle *filErr = [stdErrPipe fileHandleForReading];

[task launch];
NSData *data = [file readDataToEndOfFile];
NSData *dataError = [filErr readDataToEndOfFile];
[task waitUntilExit];

NSInteger exitCode = task.terminationStatus;

if (exitCode != 0)
{
    NSString *outputErr = [[NSString alloc] initWithData:dataError encoding:NSUTF8StringEncoding];
    NSLog(@"Error!");
    return [@"Error!" stringByAppendingString:outputErr];
}
NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return output;
}
0

There are 0 best solutions below