他にも使用できるクラスがあることは知っていますが、LWP を使用したいと考えています。ここで受け入れられた回答 ( Perl で並列 HTTP リクエストを作成し、順番に受信するにはどうすればよいですか? ) は、私が望むことを行いますが、リクエストとレスポンスを一致させるには、スレッドに「タグを付ける」必要があります。コードを次のように変更しました。
#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{} のサブはこれを行う必要があります (ただし、そうではありません)。スレッド配列をループし、それが完了すると、すべてのスレッドを実行する必要があります。次に、バンドルを調べて何かを行いますが、「印刷テスト」は決して起動しません。私はこれについて間違って考えていますか?
編集:
コメントごとに値を返そうとしましたが、これも機能しません
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];
}