我有一个PHP脚本,其中有多个sleep()
命令。我想用NSTask
在我的应用程序中执行它。我的脚本如下所示:
echo "first\n"; sleep(1); echo "second\n"; sleep(1); echo "third\n";
我可以使用通知异步执行我的任务:
- (void)awakeFromNib {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/php"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-r", @"echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";", nil];
[task setArguments: arguments];
NSPipe *p = [NSPipe pipe];
[task setStandardOutput:p];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExited:) name:NSTaskDidTerminateNotification object:task];
[task launch];
}
- (void)taskExited:(NSNotification *)notif {
NSTask *task = [notif object];
NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",str);
}
我的输出是(当然是在2秒之后):
2011-08-03 20:45:19.474 MyApp[3737:903] first
second
third
我的问题是:这三个单词印刷出来后,我如何才能立即得到它们?
发布于 2011-08-04 03:06:01
您可以使用NSFileHandle的waitForDataInBackgroundAndNotify
方法在脚本将数据写入其输出时接收通知。但是,只有在解释器立即发送字符串的情况下,这才能起作用。如果它缓冲了输出,您将在任务退出后收到一条通知。
- (void)awakeFromNib {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/php"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-r", @"echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";", nil];
[task setArguments: arguments];
NSPipe *p = [NSPipe pipe];
[task setStandardOutput:p];
NSFileHandle *fh = [p fileHandleForReading];
[fh waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:) name:NSFileHandleDataAvailableNotification object:fh];
[task launch];
}
- (void)receivedData:(NSNotification *)notif {
NSFileHandle *fh = [notif object];
NSData *data = [fh availableData];
if (data.length > 0) { // if data is found, re-register for more data (and print)
[fh waitForDataInBackgroundAndNotify];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@" ,str);
}
}
发布于 2014-10-03 11:41:43
作为参考,这里是ughoavgfhw在swift中的答案。
override func awakeFromNib() {
// Setup the task
let task = NSTask()
task.launchPath = "/usr/bin/php"
task.arguments = ["-r", "echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";"]
// Pipe the standard out to an NSPipe, and set it to notify us when it gets data
let pipe = NSPipe()
task.standardOutput = pipe
let fh = pipe.fileHandleForReading
fh.waitForDataInBackgroundAndNotify()
// Set up the observer function
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "receivedData:", name: NSFileHandleDataAvailableNotification, object: nil)
// You can also set a function to fire after the task terminates
task.terminationHandler = {task -> Void in
// Handle the task ending here
}
task.launch()
}
func receivedData(notif : NSNotification) {
// Unpack the FileHandle from the notification
let fh:NSFileHandle = notif.object as NSFileHandle
// Get the data from the FileHandle
let data = fh.availableData
// Only deal with the data if it actually exists
if data.length > 1 {
// Since we just got the notification from fh, we must tell it to notify us again when it gets more data
fh.waitForDataInBackgroundAndNotify()
// Convert the data into a string
let string = NSString(data: data, encoding: NSASCIIStringEncoding)
println(string!)
}
}
如果您的任务在管道中生成了大量输出,则此构造将是必要的。简单地调用pipe.fileHandleForReading.readDataToEndOfFile()
是行不通的,因为任务正在等待管道清空,这样它就可以在程序等待数据结束时写入更多内容。因此,您的程序将挂起。这种通知和观察者构造允许管道异步读取,从而防止了前面提到的僵局。
https://stackoverflow.com/questions/6931732
复制相似问题