bod*_*ydo 6 perl asynchronous http
我正在做很多HTTP请求,我选择HTTP :: Async来完成这项工作.我有超过1000个请求,如果我只是执行以下操作(请参阅下面的代码),许多请求在处理时会超时,因为在处理它们之前可能需要几十分钟:
for my $url (@urls) {
$async->add(HTTP::Request->new(GET => $url));
}
while (my $resp = $async->wait_for_next_response) {
# use $resp
}
Run Code Online (Sandbox Code Playgroud)
所以我决定每次做25个请求,但我想不出用代码表达它的方法.
我尝试了以下方法:
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) {
# use $resp
}
}
Run Code Online (Sandbox Code Playgroud)
然而这并不好用,因为它现在太慢了.现在它等待所有25个请求都被处理,直到它再添加25个.所以如果它还有2个请求,它什么都不做.我要等待处理所有请求以添加下一批25.
我如何改进这种逻辑,以便$async在处理记录时做一些事情,同时也确保它们不会超时.
你已经很接近了,你只需要结合这两种方法就可以了!:-)
未经测试,因此将其视为伪代码。特别是我不确定是否total_count是正确的使用方法,文档没有说明。您也可以在添加请求和收到响应时使用一个$active_requests计数器。++--
while (1) {
# if there aren't already 25 requests "active", then add more
while (@urls and $async->total_count < 25) {
my $url = shift @urls;
$async->add( ... );
}
# deal with any finished requests right away, we wait for a
# second just so we don't spin in the main loop too fast.
while (my $response = $async->wait_for_next_response(1)) {
# use $response
}
# finish the main loop when there's no more work
last unless ($async->total_count or @urls);
}
Run Code Online (Sandbox Code Playgroud)