【发布时间】:2011-09-16 17:11:45
【问题描述】:
假设使用以下代码创建句柄:
use IO::File;
my $fh = IO::File->new;
my $pid = $fh->open('some_long_running_proc |') or die $!;
$fh->autoflush(1);
$fh->blocking(0);
然后用这样的循环读取:
while (some_condition_here) {
my @lines = $fh->getlines;
...
sleep 1;
}
如果管道另一端的进程已终止,我将返回 false 的 some_condition_here 是什么?
测试$fh->eof 将不起作用,因为该进程仍可以在不打印任何新行的情况下运行。测试$fh->opened 似乎没有任何用处。
目前我正在使用$pid =! waitpid($pid, WNOHANG),它似乎在符合 POSIX 的环境中工作。这是最好的方法吗?在 Windows 上呢?
【问题讨论】:
-
$fh->blocking(0)甚至可以在 Windows 上运行吗?我认为有 some heavy wizardry 参与让非阻塞 I/O 工作。