gpt4 book ai didi

perl - IO::Lambda 在 Perl 中

转载 作者:可可西里 更新时间:2023-11-01 15:30:02 24 4
gpt4 key购买 nike

我已经分担了几个 Perl 脚本的维护任务。其中一项要求是并行下载几十个文件 (HTTP)。我继续在 CPAN 上寻找最简单的解决方案,并找到了这个名为 IO::Lambda::HTTP 的模块。 .

不幸的是,我完全没有函数式编程的经验(和初学者级别的 Perl 经验),所以虽然我看到所有示例都按照文档工作,但我无法真正修改它们中的任何一个以满足我的需要。

例如模块自带的示例:

#!/usr/bin/perl
# $Id: parallel.pl,v 1.7 2008/05/06 20:41:33 dk Exp $
#
# This example fetches two pages in parallel, one with http/1.0 another with
# http/1.1 . The idea is to demonstrate three different ways of doing so, by
# using object API, and explicit and implicit loop unrolling
#

use lib qw(./lib);
use HTTP::Request;
use IO::Lambda qw(:lambda);
use IO::Lambda::HTTP qw(http_request);
use LWP::ConnCache;

my $a = HTTP::Request-> new(
GET => "http://www.perl.com/",
);
$a-> protocol('HTTP/1.1');
$a-> headers-> header( Host => $a-> uri-> host);

my @chain = (
$a,
HTTP::Request-> new(GET => "http://www.perl.com/"),
);

sub report
{
my ( $result) = @_;
if ( ref($result) and ref($result) eq 'HTTP::Response') {
print "good:", length($result-> content), "\n";
} else {
print "bad:$result\n";
}
# print $result-> content;
}

my $style;
#$style = 'object';
#$style = 'explicit';
$style = 'implicit';

# $IO::Lambda::DEBUG++; # uncomment this to see that it indeed goes parallel

if ( $style eq 'object') {
## object API, all references and bindings are explicit
sub handle {
shift;
report(@_);
}
my $master = IO::Lambda-> new;
for ( @chain) {
my $lambda = IO::Lambda::HTTP-> new( $_ );
$master-> watch_lambda( $lambda, \&handle);
}
run IO::Lambda;
} elsif ( $style eq 'explicit') {
#
# Functional API, based on context() calls. context is
# $obj and whatever arguments the current call needs, a RPN of sorts.
# The context though is not stack in this analogy, because it stays
# as is in the callback
#
# Explicit loop unrolling - we know that we have exactly 2 steps
# It's not practical in this case, but it is when a (network) protocol
# relies on precise series of reads and writes
this lambda {
context $chain[0];
http_request \&report;
context $chain[1];
http_request \&report;
};
this-> wait;
} else {
# implicit loop - we don't know how many states we need
#
# also, use 'tail'
this lambda {
context map { IO::Lambda::HTTP-> new( $_, async_dns => 1 ) } @chain;
tails { report $_ for @_ };
};
this-> wait;
}

像宣传的那样工作,但我终其一生都无法弄清楚如何修改“对象”或“隐式”示例以将其限制为 N 个并行实例,如下 IO::Lambda 的概要:

# http://search.cpan.org/~karasik/IO-Lambda/lib/IO/Lambda.pm
# crawl for all urls in parallel, but keep 10 parallel connections max
print par(10)-> wait(map { http($_) } @hosts);

谁能给我一个例子,说明在上述约束条件下(例如限制为 N 个实例),lambda 代码会是什么样子?

此外,开始学习函数式编程的最佳方式是什么?这对我来说似乎完全陌生。

最佳答案

对于此任务,除了 IO::Lambda 之外,还有其他不错的选择,例如 AnyEvent::HTTP .参见 this previous SO question .

即使我熟悉函数式编程,上面的 IO::Lambda 示例代码对我来说还是很难理解。

关于perl - IO::Lambda 在 Perl 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4373858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com