【问题标题】:Async execution of shell command not working properlyshell命令的异步执行无法正常工作
【发布时间】:2012-04-01 14:58:00
【问题描述】:

所以,这是我的代码:

- (void)runCmd:(NSString *)cmd withArgs:(NSArray *)args
{
    NSLog(@"\nRunning ::\n\tCmd : %@\n\tArgs : %@",cmd, args);
    [theSpinner start];
    if (task)
    {
        [task interrupt];

    }
    else
    {
        task = [[NSTask alloc] init];
        [task setLaunchPath:cmd];

        [task setArguments:args];

        [pipe release];

        pipe = [[NSPipe alloc] init];
        [task setStandardOutput:pipe];

        NSFileHandle* fh = [pipe fileHandleForReading];

        NSNotificationCenter* nc;

        nc = [NSNotificationCenter defaultCenter];
        [nc removeObserver:self];
        [nc addObserver:self 
               selector:@selector(dataReady:) 
                   name:NSFileHandleReadCompletionNotification 
                 object:fh];
        [nc addObserver:self selector:@selector(dataAvailable:) name:NSFileHandleDataAvailableNotification object:fh];
        [nc addObserver:self 
               selector:@selector(taskTerminated:) 
                   name:NSTaskDidTerminateNotification 
                 object:task];

        [task launch];
        [fh readInBackgroundAndNotify];
    }
}

- (void)dataAvailable:(NSNotification*)n
{
    NSLog(@"Data Available : %@",n);
}

- (void)dataReady:(NSNotification*)n
{
    NSData* d;

    d = [[n userInfo] valueForKey:NSFileHandleNotificationDataItem];

    NSLog(@"Data Ready : %@",n);

    if ([d length])
    {
        NSLog(@"Data Ready : %@",[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding]);
    }
}

- (void) taskTerminated:(NSNotification*)note
{
    NSLog(@"Task Terminated : %@",note);
    [task release];
    task = nil;
    [theSpinner stop];

    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
    [alert setMessageText:[NSString stringWithFormat:@"Command finished"]];

    [alert runModal];
}

我尝试过运行带有参数的命令(例如 /usr/bin/php 的 php 解释器)(例如要由 php test.php 解释的文件)。

事情是这样的:

  • 脚本运行良好
  • 但是,我收到了Data ReadyTask Terminated 在我设法获得所有输出之前通知。 (我是说, dataReady: 函数仅获取
    输出,其余部分无处可寻...)

我基本上想在命令运行时异步读取所有输出。

有什么想法吗?我做错了什么?

谢谢!

【问题讨论】:

    标签: objective-c cocoa asynchronous nstask nsfilehandle


    【解决方案1】:

    您使用readInBackgroundAndNotify 来安排您的阅读。此方法仅读取一个充满数据的缓冲区并通知。您需要在通知方法中再次调用readInBackgroundAndNotify 以读取更多数据,或者如果您想一次接收所有数据,则需要使用readToEndOfFileInBackgroundAndNotify

    【讨论】:

    • 绝对正确。 (顺便说一句,我自己想通了,但还是非常感谢!;-))
    【解决方案2】:

    自 10.7 以来有一个新的 API,因此您可以避免使用 NSNotifications。

    task.standardOutput = [NSPipe pipe];
    [[task.standardOutput fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) {
        NSData *data = [file availableData]; // this will read to EOF, so call only once
        NSLog(@"Task output! %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    
        // if you're collecting the whole output of a task, you may store it on a property
        [self.taskOutput appendData:data];
    }];
    

    重要提示:

    当您的任务终止时,您必须将 readabilityHandler 块设置为 nil;否则,您将遇到高 CPU 使用率,因为读取将永远不会停止。

    [task setTerminationHandler:^(NSTask *task) {
    
        // do your stuff on completion
    
        [task.standardOutput fileHandleForReading].readabilityHandler = nil;
        [task.standardError fileHandleForReading].readabilityHandler = nil;
    }];
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多