【问题标题】:Perl simple webserver not handling multiple requests simultaneouslyPerl 简单的网络服务器不能同时处理多个请求
【发布时间】:2019-03-04 05:58:08
【问题描述】:

我写了一个简单的网络服务器,它应该不断地处理同时的请求。但是,即使在分离线程之后,它也不会处理同时请求。有人可以帮忙吗?

网络服务器.pl

use HTTP::Daemon;
use threads;

my $webServer;
my $package_map = {"test"  => "test"};
my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0],
                          LocalPort => 80,
                          Listen => 20) || die;

print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ", $d->sockport(), "\n";


while (my $c = $d->accept) {
    threads->create(\&process_req, $c)->detach();
}

sub process_req {
    my $c = shift;
    my $r = $c->get_request;
    if ($r) {
        if ($r->method eq "GET") {
            my $path = $r->url->path();
            my $service = $package_map->{$path};
            if ($service) {
                $response = $service->process_request($request);
            }
        }
    }
    $c->close;
    undef($c);
}

test.pm

sub process_request
{
    threads->create(\&testing)->detach();
    my $response = HTTP::Response -> new (200);
    $response -> header('Access-Control-Allow-Origin', '*');
    $response -> content("Success");
    return $response; 
}

sub testing
{
    my $command = 'echo "sleep 100" | ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 <dev_box>';
    if (system($command) != 0) {
        print "FAILED\n";
    }
}

【问题讨论】:

  • 您应该添加一些print 语句,以便查看网络服务器挂起的位置。 HTTP::Daemon 建议一个连接的 while 循环:while (my $r = $c-&gt;get_request) {
  • 它没有挂起,而是休眠 100 秒,然后再次开始接受请求。
  • 我无法复制您的问题。当用sleep 100 替换您的子模块并实际调用$c-&gt;send_response() 时,我可以连接至少三个并行HTTP 请求,每个请求在100 秒后得到处理。我将在下面发布工作代码作为回复。
  • perl -v | grep 'This is' ; perl -V:useithreads 的输出是什么?

标签: multithreading perl http-daemon


【解决方案1】:

这是基于您的示例的代码,适用于我在 Windows 上。也许您可以向我们展示它是如何/在哪里失败的:

#!perl
use strict;
use warnings;
use HTTP::Daemon;
use threads;

my $webServer;
#my $package_map = {"test"  => "test"};
my $d = HTTP::Daemon->new(LocalAddr => $ARGV[0],
                          LocalPort => $ARGV[1] // 80,
                          Listen => 20) || die;

print "Web Server started!\n";
print "Server Address: ", $d->sockhost(), "\n";
print "Server Port: ", $d->sockport(), "\n";


while (my $c = $d->accept) {
    warn "New connection";
    threads->create(\&process_req, $c)->detach();
}

sub process_req {
    my $c = shift;
    while( my $r = $c->get_request ) {
        if ($r) {
            if ($r->method eq "GET") {
                my $path = $r->url->path();
                if (1) {
                    sleep 100;
                    $c->send_response( HTTP::Response->new(200, "OK", undef, "done\n") );
                }
            }
        }
    };
    $c->close;
}

【讨论】:

  • 当你在 process_req 函数中创建另一个线程时,多个请求不会同时工作。
  • 您能否编辑我的示例或您的示例以实际重现问题,同时仍位于单个文件中?至少在 Windows 上,我无法用给出的代码重现您的问题。
  • Works fine 在 Linux 中也是如此。 @Soumya dutta,你确定你有 Perl 的线程构建吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-09
  • 2011-07-11
  • 2018-04-16
  • 1970-01-01
相关资源
最近更新 更多