objective-c - 具有长输出的 NSTask (OSX)

标签 objective-c macos cocoa xcode4.2

我一直在寻找答案很长一段时间,但我从来没有找到有效的代码。

我有一个使用 dd 生成文件的代码。有时需要几分钟,具体取决于大小,我认为有一个进度条会很棒。到目前为止,一切正常并且被捆绑。但是,进度条不会更新,因为我需要不断向它发送值。我找到了获取当前值的方法,我设法让 pv 显示当前数据,但现在我无法在应用程序内部实时获取输出,除了在日志中。

到目前为止,这是我得到的最好结果:

// Action:
// dd if=/dev/zero bs=1048576 count=500 |pv -Wn -s <size>|of=/Users/me/Desktop/asd.img
// Be careful, it generates files of 500MB!!

NSTask * d1Task = [[NSTask alloc] init];
NSTask * pvTask = [[NSTask alloc] init];
NSTask * d2Task = [[NSTask alloc] init];

[d1Task setLaunchPath:@"/bin/dd"];
[pvTask setLaunchPath:@"/Users/me/Desktop/pv"];
[d2Task setLaunchPath:@"/bin/dd"];

// For pvTask I use  something like...
// [[NSBundle mainBundle] pathForAuxiliaryExecutable: @"pv"]
// ... in the final version.

[d1Task setArguments: [NSArray arrayWithObjects:
                         @"if=/dev/zero"
                       , @"bs=1048576"
                       , @"count=500"
                       , nil]];

[pvTask setArguments:[NSArray arrayWithObjects:
                        @"-Wn"
                      , [ NSString stringWithFormat:@"-s %d", 1048576 * 500]
                      , nil]];

[d2Task setArguments: [NSArray arrayWithObjects:
                        @"of=/Users/me/Desktop/file.dmg"
                      , nil]];

NSPipe * pipeBetween1 = [NSPipe pipe];
[d1Task setStandardOutput: pipeBetween1];
[pvTask setStandardInput: pipeBetween1];

NSPipe * pipeBetween2 = [NSPipe pipe];
[pvTask setStandardOutput: pipeBetween2];
[d2Task setStandardInput: pipeBetween2];

// Missing code here

[d1Task launch];
[pvTask launch];
[d2Task launch];

在缺少的代码部分,我尝试了几件事。首先我尝试了一个观察者,像这样:

NSFileHandle * pvOutput = [pipeBetween2 fileHandleForReading];
[pvOutput readInBackgroundAndNotify];

[ [NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(outputData:)
            name:NSFileHandleDataAvailableNotification
          object:pvOutput
];

没有成功。我只在执行的开始或结束时得到反馈,但在执行期间仍然没有任何反馈。

我也尝试过类似的东西:

[pvOutput setReadabilityHandler:^(NSFileHandle *file) {
// Stuff here
}];

但是这里没有这个方法。也许我的 XCode 已经过时了? (我用的是 4.2)。

最近我一直在尝试使用 /sbin/ping 对服务器执行 10 次 ping 操作,但没有成功获得输出。我怎样才能做到这一点?我可以阅读有关此主题的任何文档吗?

谢谢!

最佳答案

pv 工具将进度输出写入标准错误,因此您应该建立 另一个管道:

NSPipe *pvStderrPipe = [NSPipe pipe];
[pvTask setStandardError:pvStderrPipe];
NSFileHandle *pvError = [pvStderrPipe fileHandleForReading];

[pvError waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification
                          object:pvError queue:nil
                          usingBlock:^(NSNotification *note) 
{
     NSData *progressData = [pvError availableData];
     NSString *progressStr = [[NSString alloc] initWithData:progressData encoding:NSUTF8StringEncoding];
     NSLog(@"progress: %@", progressStr);

     [pvError waitForDataInBackgroundAndNotify];
 }];

一个完全不同的解决方案可能是使用“dd”的以下特性:

If dd receives a SIGINFO signal, the current input and output block counts will be written to the standard error output in the same format as the standard completion message.

因此您可以将管道连接到“dd”的标准错误并调用

kill([ddTask processIdentifier], SIGINFO);

定期。那么你不需要“pv”,可能只需要一个“dd”任务。

关于objective-c - 具有长输出的 NSTask (OSX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913914/

相关文章:

cocoa - 如何在 Mac OS X 中测试有效的文件名

cocoa - cocoa/objective-c 中的时钟控制

iphone - 如何在 iPhone 的 UIbutton 中限制属性的访问?

iphone - 调用实例方法通常比调用类方法更快吗?

macos - OSX 上的 XAMPP 和 MySQL 安装

macos - 沙箱和安全范围的书签

macos - Mac终端killall命令是退出还是强制退出?

ios - 实现 Inneractive/Inneractive 横幅广告 - 广告定位问题

ios - 在当前线程上执行 Parse.com 用户注册 (signUp :) and retrieving the NSError

macos - SDL-2:在 OS X 中运行 native cocoa 应用程序时出现问题