我正在使用 Perl 库 HTTP::Async如下:
use strict;
use warnings;
use HTTP::Async;
use Time::HiRes;
...
my $async = HTTP::Async->new( ... );
my $request = HTTP::Request->new( GET => $url );
my $start = [Time::HiRes::gettimeofday()];
my $id = $async->add($request);
my $response = undef;
while (!$response) {
$response = $async->wait_for_next_response(1);
last if Time::HiRes::tv_interval($start) > TIME_OUT;
}
...
当 while
循环超时且脚本结束时,我遇到以下错误消息:
HTTP::Async object destroyed but still in use at script.pl line 0
HTTP::Async INTERNAL ERROR: 'id_opts' not empty at script.pl line 0
我有什么选择?如果仍在使用但不再需要,如何“清理” HTTP::Async 对象?
最佳答案
我建议您删除
不完整的请求,但该模块没有提供任何接口(interface)来执行此操作。
选项 1:添加删除功能。
将以下内容添加到您的脚本中:
BEGIN {
require HTTP::Async;
package HTTP::Async;
if (!defined(&remove)) {
*remove = sub {
my ($self, $id) = @_;
my $hashref = $self->{in_progress}{$id}
or return undef;
my $s = $hashref->{handle};
$self->_io_select->remove($s);
delete $self->{fileno_to_id}{ $s->fileno };
delete $self->{in_progress}{$id};
delete $self->{id_opts}{$id};
return $hashref->{request};
};
}
if (!defined(&remove_all)) {
*remove_all = sub {
my ($self) = @_;
return map $self->remove($_), keys %{ $self->{in_progress} };
};
}
}
您应该联系作者,看看他是否可以添加此功能。 $id
是 add
返回的值。
选项 2:静默来自析构函数的所有警告。
如果您同意不满足所有请求,那么消除警告也没有什么坏处。您可以按如下方式进行操作:
use Sub::ScopeFinalizer qw( scope_finalizer );
my $async = ...;
my $anchor = scope_finalizer {
local $SIG{__WARN__} = sub { };
$async = undef;
};
...
请注意,这将消除对象销毁期间发生的所有警告,因此我不太喜欢这样做。
关于perl - 如何清理仍在使用的 HTTP::Async,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587962/