Eri*_*rom 5 perl file-io ipc pipe
假设使用以下代码创建句柄:
use IO::File;
my $fh = IO::File->new;
my $pid = $fh->open('some_long_running_proc |') or die $!;
$fh->autoflush(1);
$fh->blocking(0);
Run Code Online (Sandbox Code Playgroud)
然后用这样的循环读取:
while (some_condition_here) {
my @lines = $fh->getlines;
...
sleep 1;
}
Run Code Online (Sandbox Code Playgroud)
some_condition_here
如果管道另一端的进程终止,那么我会放弃什么?
测试$fh->eof
不起作用,因为该过程仍然可以在不打印任何新行的情况下运行.测试$fh->opened
似乎没有做任何有用的事情.
目前我正在使用$pid =! waitpid($pid, WNOHANG)
它似乎在符合POSIX的环境中工作.这是最好的方法吗?在Windows上怎么样?
在使用时select
,
use strict;
use warnings;
use IO::Select qw( );
sub process_msg {
my ($client, $msg) = @_;
chomp $msg;
print "$client->{id} said '$msg'\n";
return 1; # Return false to close the handle.
}
my $select = IO::Select->new();
my %clients;
for (...) {
my $fh = ...;
$clients{fileno($fh)} = {
id => '...'
buf => '',
# ...
};
$select->add($fh);
}
while (my @ready = $select->can_read) {
for my $fh (@ready) {
my $client = $clients{ fileno($fh) };
our $buf; local *buf = \( $client->{buf} );
my $rv = sysread($fh, $buf, 64*1024, length($buf));
if (!$rv) {
if (defined($rv)) {
print "[$client->{id} ended]\n";
} else {
print "[Error reading from $client->{id}: $!]\n";
}
print "[Incomplete message received from $client->{id}]\n"
if length($buf);
delete $clients{ fileno($fh) };
$select->remove($fh);
next;
}
while ($buf =~ s/^(.*\n)//) {
if (!process_msg($client, "$1")) {
print "[Dropping $client->{id}]\n";
delete $clients{ fileno($fh) };
$select->remove($fh);
last;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1847 次 |
最近记录: |