我知道我可以使用其他类,但我想使用LWP。这里接受的答案(How do I make parallel HTTP requests in Perl, and receive them back in order?)符合我的要求,但我需要“标记”线程,以便我可以将请求与响应相匹配。我已将代码修改为以下内容:
#Setup and query the DB:
#hold all response\id pairs
my @response_bundles;
while (my @row = $query->fetchrow_array()) {
my $id = $row[0];
my $url = $row[1];
push @threads, async {
my @container_hash;
$container_hash[0] = @row;
$container_hash[1] = $ua->get($url);
push @response_bundles, @container_hash;
};
}
#Once this loop terminates all threads are done
for my $thread (@threads) {
$thread->join;
}
#Since all threads are completed we can now sort through the response bundles
for my $response_bundle (@response_bundles) {
print "test\n";
}
我的目标是启动一堆HTTP请求并将其($ id,$ response)对存储在一个数组中。我将它们全部推入异步进程,async {}中的sub应该这样做(但事实并非如此)。我遍历线程数组,一旦完成,所有线程都应该完成。然后,我通过我的捆绑并做一些事情然而“打印测试”从未开始。我在考虑这个错误吗?
编辑:
根据评论我尝试返回值,但这不起作用
while (my @row = $query->fetchrow_array()) {
my $id = $row[0];
my $url = $row[1];
push @threads, async {
my @container_hash;
$container_hash[0] = @row;
$container_hash[1] = $ua->get($url);
return @container_hash;
};
}
#Once this loop terminates all threads are done
for my $thread (@threads) {
my @container;
@container = $thread->join;
print $container[1];
}
答案 0 :(得分:1)
您需要return
线程中所需的数据,以便主程序可以处理它:
while (my @row = $query->fetchrow_array()) {
my $id = $row[0];
my $url = $row[1];
push @threads, async {
my @container_hash;
$container_hash[0] = \@row; #I think you need this
$container_hash[1] = $ua->get($url);
return \@container_hash; #and here we return reference to our data
};
}
#Once this loop terminates all threads are done
for my $thread (@threads) {
my $container = $thread->join;
print $container->[1], "\n"; #this is a reference that's why we use ->
}