I make a lot of HTTP requests and I selected HTTP :: Async to complete the job. I have more than 1000 requests, and if I just do the following (see the code below), many requests will have a timeout before they are processed, because it can take up to tens of minutes before processing takes them:
for my $url (@urls) { $async->add(HTTP::Request->new(GET => $url)); } while (my $resp = $async->wait_for_next_response) {
So I decided to make 25 queries at a time, but I can't think of a way to express this in code.
I tried the following:
while (1) { L25: for (1..25) { my $url = shift @urls; if (!defined($url)) { last L25; } $async->add(HTTP::Request->new(GET => $url)); } while (my $resp = $async->wait_for_next_response) {
This, however, is not so good, because now it is too slow. Now he waits until all 25 requests are processed, until he adds another 25. So if he has 2 requests left, he does nothing. I have to wait for all requests to be processed to add the next batch of 25.
How could I improve this logic to make $async
do something while I process the records, but also make sure they are not timeout.
source share