gpt4 book ai didi

objective-c - 无法从 NSTask 的标准输出获取中间输出?

转载 作者:行者123 更新时间:2023-12-03 17:38:50 24 4
gpt4 key购买 nike

我为一个简单的 python 脚本编写了一个 NSTask 异步执行方法。

当 python 脚本打印到标准输出时,一切都很好。当其中有 raw_input 时(期望来自用户的输入),它确实可以很好地获取输入,但它不会在 raw_input 之前打印数据。

发生什么事了?

- (NSString*)exec:(NSArray *)args environment:(NSDictionary*)env action:(void (^)(NSString*))action completed:(void (^)(NSString*))completed
{
_task = [NSTask new];
_output = [NSPipe new];
_error = [NSPipe new];
_input = [NSPipe new];
NSFileHandle* outputF = [_output fileHandleForReading];
NSFileHandle* errorF = [_error fileHandleForReading];
NSFileHandle* inputF = [_input fileHandleForWriting];

__block NSString* fullOutput = @"";

NSMutableDictionary* envs = [NSMutableDictionary dictionary];

NSArray* newArgs = @[@"bash",@"-c"];

[_task setLaunchPath:@"/usr/bin/env"];
if (env)
for (NSString* key in env)
envs[key] = env[key];

if ([envs count]) [_task setEnvironment:envs];

NSString* cmd = @"";

cmd = [cmd stringByAppendingString:[[[self sanitizedArgs:args] componentsJoinedByString:@" "] stringByAppendingString:@" && echo \":::::$PWD:::::\""]];

[_task setArguments:[newArgs arrayByAddingObject:cmd]];
[_task setStandardOutput:_output];
[_task setStandardError:_error];
[_task setStandardInput:_input];

void (^outputter)(NSFileHandle*) = ^(NSFileHandle *file){
NSData *data = [file availableData];
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"Output: %@",str);

action(str);
fullOutput = [fullOutput stringByAppendingString:str];
};

void (^inputter)(NSFileHandle*) = ^(NSFileHandle *file) {
NSLog(@"In inputter");
NSData *data = [[_task.standardOutput fileHandleForReading] availableData];
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"Output: %@",str);
};

[outputF setReadabilityHandler:outputter];
[errorF setReadabilityHandler:outputter];
//[inputF setWriteabilityHandler:inputter];

[_task setTerminationHandler:^(NSTask* task){
NSLog(@"Terminated: %@",fullOutput);
completed(fullOutput);

//[task.standardOutput fileHandleForReading].readabilityHandler = nil;
//[task.standardError fileHandleForReading].readabilityHandler = nil;
//[task.standardInput fileHandleForWriting].writeabilityHandler = nil;
//[task terminate];
//task = nil;
}];

[_task launch];

//[[_input fileHandleForWriting] waitForDataInBackgroundAndNotify];

return @"";
}
<小时/>

P.S.我到处寻找解决方案,但似乎没有发现任何东西。看起来有大量的 NSTask 演练和教程,但是 - 有趣的巧合 - 他们通常避免处理任何 stdin 含义

最佳答案

这与父进程(具有 NSTask 对象的进程)没有任何关系。这都是关于子进程的行为。子进程实际上还没有将字节写入管道。

来自stdio man page :

Initially, the standard error stream is unbuffered; the standard input and output streams are fully buffered if and only if the streams do not refer to an interactive or “terminal” device, as determined by the isatty(3) function. In fact, all freshly-opened streams that refer to terminal devices default to line buffering, and pending output to such streams is written automatically whenever such an input stream is read. Note that this applies only to ``true reads''; if the read request can be satisfied by existing buffered data, no automatic flush will occur. In these cases, or when a large amount of computation is done after printing part of a line on an output terminal, it is necessary to fflush(3) the standard output before going off and computing so that the output will appear. Alternatively, these defaults may be modified via the setvbuf(3) function.

就您而言,标准输入和输出实际上并不指交互式/终端设备。因此,标准输出是完全缓冲的(也称为 block 缓冲,而不是行缓冲或无缓冲)。仅当 stdio 库内部缓冲区已满、关闭流或在流上调用 fflush() 时,才会刷新它。

您所期望的行为,即读取输入时自动刷新标准输出,仅在流连接到交互式/终端设备的情况下发生。

除非使用伪终端设备而不是管道进行输入和输出,否则父进程无法影响子进程的缓冲行为。然而,这是一项复杂且容易出错的工作。除此之外,子进程还必须编写代码来设置标准输出的缓冲模式或定期刷新它。

关于objective-c - 无法从 NSTask 的标准输出获取中间输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com