1

我有 unix socket server.pl 的 perl 脚本

use IO::Select;
use IO::Socket;

$lsn = new IO::Socket::INET(Listen => 512, LocalPort => 8888);
my $socket_path = '/tmp/punix.sock';
unlink($socket_path);

$SIG{PIPE} = 'IGNORE';
$|++;
$lsn = IO::Socket::UNIX->new(
Type   => SOCK_STREAM,
Local  => $socket_path,
Listen => 512,
) or die("Can't create server socket: $!\n");

$sel = new IO::Select( $lsn );

while(@ready = $sel->can_read) {
 foreach $fh (@ready) {
    if($fh == $lsn) {
        # Create a new socket
        $new = $lsn->accept;
        $sel->add($new);
    }
    else {
        # Process socket
        my $input = <$fh>;
        #........ do some work
        # 
        # Maybe we have finished with the socket
        $sel->remove($fh);
        $fh->close;
    }
}
}

并且客户端正在并行连接到套接字并获得结果。对于前几个连接来说,这工作正常且快速,例如 100 个连接中的 60 个连接,之后其余 40 个连接的处理速度很慢,例如每秒 1 个。server.pl 似乎没有任何泄漏/问题。可能是什么原因。我试过 Event::Lib 也是同样的问题。

4

1 回答 1

1

可能与my $input = <$fh>;. 那是错误的。它阻塞直到收到换行符。您只能安全地使用sysread.

our $buf; local *buf = \$bufs{$fh};   # Creates alias $buf for $bufs{$fh}

my $rv = sysread($fh, $buf, length($buf), 64*1024);
if (!defined($rv)) {
   ... handle error ...
   next;
}

if (!$rv) {
   ... handle eof ...
   next;
}

while ($buf =~ s/^(.*)\n//) {
   my $line = $1;
   ...
}
于 2013-10-12T17:17:12.677 回答