Perl 使用sysread时不可用sysread将返回可用内容,不再返回。我会在帖子中补充这一点。我的意思是,既然我确切地知道文件句柄上返回的是什么(并且一次返回所有文件),缓冲区真的很重要吗?@Lomsky,你关于一次返回所有文件的假设是不正确的。你无法控制。你
Perl 使用sysread时不可用sysread将返回可用内容,不再返回。我会在帖子中补充这一点。我的意思是,既然我确切地知道文件句柄上返回的是什么(并且一次返回所有文件),缓冲区真的很重要吗?@Lomsky,你关于一次返回所有文件的假设是不正确的。你无法控制。你,perl,io,buffer,Perl,Io,Buffer,使用sysread时不可用sysread将返回可用内容,不再返回。我会在帖子中补充这一点。我的意思是,既然我确切地知道文件句柄上返回的是什么(并且一次返回所有文件),缓冲区真的很重要吗?@Lomsky,你关于一次返回所有文件的假设是不正确的。你无法控制。你知道发送的是什么,而不是收到的是什么。如果您知道收到了什么,那么就没有理由使用select@Lomsky,这补充了解释为什么在我声称“缓冲不好”时使用您自己的缓冲区是安全的。 my $read_set = IO::Select()->ne
使用
sysread
时不可用sysread
将返回可用内容,不再返回。我会在帖子中补充这一点。我的意思是,既然我确切地知道文件句柄上返回的是什么(并且一次返回所有文件),缓冲区真的很重要吗?@Lomsky,你关于一次返回所有文件的假设是不正确的。你无法控制。你知道发送的是什么,而不是收到的是什么。如果您知道收到了什么,那么就没有理由使用select
@Lomsky,这补充了解释为什么在我声称“缓冲不好”时使用您自己的缓冲区是安全的。
my $read_set = IO::Select()->new;
my $count = @agents_to_run; #array comes as an argument
for $agent ( @agents_to_run ) {
( $sock, my $peerhost, my $peerport )
= server($config_settings{ $agent }->
{ 'Host' },$config_settings{ $agent }->{ 'Port' };
$read_set->add( $sock );
}
while ( $count > 0) {
my @rh_set = IO::Select->can_read();
for my $rh ( @{ $rh_set } ) {
my %results = <$rh>;
my $num_files = $results{'numFiles'};
my @files = ();
for (my i; i < $num_files; i++) {
$files[i]=<$rh>;
}
#process results, close fh, decrement count, etc
}
}
use IO::Select qw( );
use IO::Handle qw( );
sub producer {
my ($fh) = @_;
for (;;) {
print($fh time(), "\n") or die;
print($fh time(), "\n") or die;
sleep(3);
}
}
sub consumer {
my ($fh) = @_;
my $sel = IO::Select->new($fh);
while ($sel->can_read()) {
my $got = <$fh>;
last if !defined($got);
chomp $got;
print("It took ", (time()-$got), " seconds to get the msg\n");
}
}
pipe(my $rfh, my $wfh) or die;
$wfh->autoflush(1);
fork() ? producer($wfh) : consumer($rfh);
It took 0 seconds to get the msg
It took 3 seconds to get the msg
It took 0 seconds to get the msg
It took 3 seconds to get the msg
It took 0 seconds to get the msg
...
sub consumer {
my ($fh) = @_;
my $sel = IO::Select->new($fh);
my $buf = '';
while ($sel->can_read()) {
sysread($fh, $buf, 64*1024, length($buf)) or last;
while ( my ($got) = $buf =~ s/^(.*)\n// ) {
print("It took ", (time()-$got), " seconds to get the msg\n");
}
}
}
It took 0 seconds to get the msg
It took 0 seconds to get the msg
It took 0 seconds to get the msg
It took 0 seconds to get the msg
It took 0 seconds to get the msg
It took 0 seconds to get the msg
...
$select->add($fh);
$clients{fileno($fh)} = {
buf => '',
...
};
while (my @ready = $select->can_read) {
for my $fh (@ready) {
my $client = $clients{fileno($fh)};
our $buf; local *buf = \($client->{buf}); # alias $buf = $client->{buf};
my $rv = sysread($fh, $buf, 64*1024, length($buf));
if (!$rv) {
if (!defined($rv)) {
... # Handle error
}
elsif (length($buf)) {
... # Handle eof with partial message
}
else {
... # Handle eof
}
delete $clients{fileno($fh)};
$sel->remove($fh);
next;
}
while ( my ($msg) = $buf =~ s/^(.*)\n// )
... # Process message.
}
}
}