Cra*_*tis 2 python stdout pipe objective-c nstask
我有一个简单的Python脚本,询问你的名字,然后吐出来:
def main():
print('Enter your name: ')
for line in sys.stdin:
print 'You entered: ' + line
Run Code Online (Sandbox Code Playgroud)
非常简单的东西!在OS X终端中运行时,它运行良好:
$ python nameTest.py
Enter your name:
Craig^D
You entered: Craig
Run Code Online (Sandbox Code Playgroud)
但是,当尝试通过a运行此过程时NSTask,只有在Python脚本中添加了额外的flush()调用时才会出现stdout.
这就是我NSTask配置和配管的方式:
NSTask *_currentTask = [[NSTask alloc] init];
_currentTask.launchPath = @"/usr/bin/python";
_currentTask.arguments = [NSArray arrayWithObject:@"nameTest.py"];
NSPipe *pipe = [[NSPipe alloc] init];
_currentTask.standardOutput = pipe;
_currentTask.standardError = pipe;
dispatch_queue_t stdout_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
__block dispatch_block_t checkBlock;
checkBlock = ^{
NSData *readData = [[pipe fileHandleForReading] availableData];
NSString *consoleOutput = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.consoleView appendString:consoleOutput];
});
if ([_currentTask isRunning]) {
[NSThread sleepForTimeInterval:0.1];
checkBlock();
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
NSData *readData = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *consoleOutput = [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding];
[self.consoleView appendString:consoleOutput];
});
}
};
dispatch_async(stdout_queue, checkBlock);
[_currentTask launch];
Run Code Online (Sandbox Code Playgroud)
但是在运行时NSTask,这就是它出现的方式(它最初是空白的,但在输入我的名字并按下CTRL + D后,它会立即完成所有操作):
Craig^DEnter your name:
You entered: Craig
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:我怎样才能读取stdout从我的NSTask,而不需要额外的flush()语句在我的Python脚本?为什么输入你的名字:提示在运行时不会立即出现NSTask?
当Python看到它的标准输出是一个终端时,它会安排sys.stdout在脚本读取时自动刷新sys.stdin.使用NSTask脚本运行脚本时,脚本的标准输出是管道,而不是终端.
有一个特定于Python的解决方案.您可以将-u标志传递给Python解释器(例如_currentTask.arguments = @[ @"-u", @"nameTest.py"];),它告诉Python不要缓冲标准输入,标准输出或标准错误.您还可以PYTHONUNBUFFERED=1在流程环境中设置以实现相同的效果.
适用于任何程序的更通用的解决方案使用所谓的"伪终端"(或者,历史上称为"伪电传"),我们将其简化为"pty".(事实上,这就是终端应用程序本身所做的.它是一种罕见的Mac,它有一个物理终端或电传打字机连接到串口!)
每个pty实际上是一对虚拟设备:从设备和主设备.您写入主站的字节,您可以从从站读取,反之亦然.因此,这些设备更像是套管(双向),而不是像管道(单向).此外,pty还允许您设置终端I/O标志(或"termios"),以控制从属设备是否回显其输入,是否一次传递其输入一行或一次传递一个字符等等.
无论如何,您可以使用该openpty功能轻松打开主/从对.这里有一个小类,您可以使用它来使NSTask对象使用从属端进行任务的标准输入和输出.
@interface NSTask (PTY)
- (NSFileHandle *)masterSideOfPTYOrError:(NSError **)error;
@end
Run Code Online (Sandbox Code Playgroud)
#import "NSTask+PTY.h"
#import <util.h>
@implementation NSTask (PTY)
- (NSFileHandle *)masterSideOfPTYOrError:(NSError *__autoreleasing *)error {
int fdMaster, fdSlave;
int rc = openpty(&fdMaster, &fdSlave, NULL, NULL, NULL);
if (rc != 0) {
if (error) {
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
}
return NULL;
}
fcntl(fdMaster, F_SETFD, FD_CLOEXEC);
fcntl(fdSlave, F_SETFD, FD_CLOEXEC);
NSFileHandle *masterHandle = [[NSFileHandle alloc] initWithFileDescriptor:fdMaster closeOnDealloc:YES];
NSFileHandle *slaveHandle = [[NSFileHandle alloc] initWithFileDescriptor:fdSlave closeOnDealloc:YES];
self.standardInput = slaveHandle;
self.standardOutput = slaveHandle;
return masterHandle;
}
@end
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
NSTask *_currentTask = [[NSTask alloc] init];
_currentTask.launchPath = @"/usr/bin/python";
_currentTask.arguments = @[[[NSBundle mainBundle] pathForResource:@"nameTest" ofType:@"py"]];
NSError *error;
NSFileHandle *masterHandle = [_currentTask masterSideOfPTYOrError:&error];
if (!masterHandle) {
NSLog(@"error: could not set up PTY for task: %@", error);
return;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以从任务中读取并使用写入任务masterHandle.
| 归档时间: |
|
| 查看次数: |
878 次 |
| 最近记录: |