为什么Windows上的Perl的IO :: Socket抱怨“资源不可用"? 64个连接后? [英] Why does Perl's IO::Socket on Windows complain about "Resource Not Available" after 64 connections?
问题描述
我用Windows下的Perl创建了一个服务器(ActivePerl 5.10.1 build 1006),该服务器在连接时分叉,接受一些JSON数据,并将其写入数据库. 64个客户端连接到服务器后,我遇到了问题,尝试分叉时错误消息为资源不可用".
I created a server with Perl under Windows (ActivePerl 5.10.1 build 1006) that forks upon being connected to, accepts some JSON data, and writes it to a database. I am running into a problem after 64 clients connect to the server, the error message being "Resource is not available" when trying to fork.
在Linux上运行此代码,我发现许多已失效的子进程,可以通过在父进程上添加wait()
调用来解决.但是,这并不能解决问题.在Linux下运行代码的工作已经超过Windows允许的64个调用.
Running this code under Linux I found many defunct child process, which was solved by adding a wait()
call on the parent. This however did not solve the problem. Running the code under Linux works past the 64 calls allowed in Windows.
如果服务器受到限制,我还会启动虚拟Windows服务器,但是重新安装Perl会导致相同的64个连接限制.
I also spun up a virtual Windows server in case it was restrictions on the server, but a fresh install of Perl resulted in the same 64 connection limit.
任何想法都值得赞赏.
use IO::Socket;
use Net::hostent;
use JSON;
use DBI;
use Data::Dumper;
my $port=shift || 9000;
my $clients_served = 0;
while(1){
my $server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $port,
Listen => 1,
Reuse => 1);
die "can't setup server" unless $server;
print "[Server $0 is running]\n";
####
# wait for a client to connect
# once it has, fork to a seperate thread and
# retrieve the JSON data
####
while (my $client = $server->accept()) {
my $pid = fork();
if ($pid != 0) {
print ("Serving client " . $clients_served++ . "\n");
}else{
$client->autoflush(1);
my $JSONObject = JSON->new->ascii->pretty->allow_nonref();
my $hostinfo = gethostbyaddr($client->peeraddr);
my $client_hostname = ($hostinfo->name || $client->peerhost);
printf "connect from %s\n", $client_hostname;
print " $client_hostname connected..\n";
syswrite($client, "Reached Server\n", 2048);
if (sysread($client, my $buffer, 2048) > 0) {
foreach my $tasks($JSONObject->decode($buffer)){
foreach my $task (@$tasks){
insert_record($client_hostname, $task); #empty method, contents does not affect result
}
}
}
print " $client_hostname disconnected..\n";
close $client;
exit 0;
}
}
$server->close();
}
exit 0;
推荐答案
尝试从完成的交易中获取僵尸进程.我可以将您的示例代码发送给 如果再添加几行,请继续运行:
Try reaping the zombie processes from the finished transactions. I can get your sample code to keep running if I include a couple more lines:
use POSIX ':sys_wait_h';
if ($pid != 0) {
print ("Serving client " . $clients_served++ . "\n");
1 while waitpid -1, WNOHANG > 0;
如果您可能有64个同时连接,则可能不得不考虑其他问题-在Windows上安装SIGCHLD
处理程序是没有好处的.
If you might have 64 simultaneous connections, you might have to think of something else -- it's no good to install a SIGCHLD
handler on Windows.
这篇关于为什么Windows上的Perl的IO :: Socket抱怨“资源不可用"? 64个连接后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!