首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何多线程查看Perl中是否存在网页?

如何多线程查看Perl中是否存在网页?
EN

Stack Overflow用户
提问于 2012-07-21 22:08:23
回答 2查看 198关注 0票数 3

我正在编写一个Perl脚本,它接收URL列表并检查它们是否存在。(请注意,我只关心它们的存在;我不在乎它们的内容。这是这个项目的重要部分。

代码语言:javascript
复制
use LWP::Simple qw($ua head);

if (head($url))
{
    $numberAlive ++;
}
else
{
    $numberDead ++;
}

现在,该程序运行良好;但是,我希望它运行得更快。因此,我正在考虑使它成为多线程。我假设程序的慢部分是为每个URL与服务器联系;因此,我正在寻找一种方法,在等待第一个响应时,可以向列表中其他网页的URL发送请求。我该怎么做?据我所知,head例程没有回调,一旦服务器响应,就可以调用回调。

EN

回答 2

Stack Overflow用户

发布于 2012-07-22 07:58:03

另一个选项是HTTP::异步。

代码语言:javascript
复制
#!/usr/bin/perl
use strict;
use warnings;

use HTTP::Request;
use HTTP::Async;

my $numberAlive = 0;
my $numberDead  = 0;
my @urls = ('http://www.perl.com','http://www.example.xyzzy/foo.html');

my $async = HTTP::Async->new;

# you might want to wrap this in a loop to deal with @urls in batches
foreach my $url (@urls){   
  $async->add( HTTP::Request->new( HEAD => $url ) );
  }

while ( my $response = $async->wait_for_next_response ) {
  if ($response->code == 200){$numberAlive ++;}
  else{$numberDead ++;}
  }

print "$numberAlive Alive, $numberDead Dead\n";
票数 3
EN

Stack Overflow用户

发布于 2012-07-22 06:36:06

基于工作人员的并行化(使用您选择的线程或进程):

代码语言:javascript
复制
use strict;
use warnings;
use feature qw( say );
use threads;  # or: use forks;

use LWP::Simple        qw( head );
use Thread::Queue::Any qw( );

use constant NUM_WORKERS => 10;  # Or whatever.

my $req_q  = Thread::Queue::Any->new();
my $resp_q = Thread::Queue::Any->new();

my @workers;
for (1..NUM_WORKERS) {
   push @workers, async {
      while (my $url = $req_q->dequeue()) {
         my $is_alive = head($url) ? 1 : 0;
         $resp_q->enqueue($is_alive);
      }
   };
}

$req_q->enqueue($_) for @urls;

my ($alive, $dead);
for (1..@urls) {
   my $is_alive = $resp_q->dequeue();
   ++( $is_alive ? $alive : $dead );
}

$req_q->enqueue(undef) for @workers;
$_->join for @workers;

say $alive;
say $dead;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11596203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档