2

我正在使用 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 对象?

4

2 回答 2

1

我建议您remove完成请求,但该模块不提供任何接口来执行此操作。


选项 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;
};
...

请注意,这将使对象销毁期间发生的所有警告静音,所以我不太喜欢这样。

于 2012-05-14T17:30:32.247 回答
1

将 HTTP::Async 子类化以获得更通用的解决方案并不难。我使用它来中止所有待处理的请求:

package HTTP::Async::WithFlush;
use strict;
use warnings;    
use base 'HTTP::Async';
use Time::HiRes qw(time);

sub _flush_to_send {
  my $self = shift;
  for my $request (@{ $self->{to_send} }) {
    delete $self->{id_opts}->{$request->[1]};
  }
  $self->{to_send} = [];
}

sub _flush_in_progress {
  my $self = shift;
  # cause all transfers to time out
  for my $id (keys %{ $self->{in_progress} }) {
    $self->{in_progress}->{$id}->{finish_by} = time - 1;
  }
  $self->_process_in_progress;
}

sub _flush_to_return {
  my $self = shift;                                                                 
  while($self->_next_response(-1)) { }
}

sub flush_pending_requests {
  my $self = shift;
  $self->_flush_to_send;
  $self->_flush_in_progress;
  $self->_flush_to_return;
  return;
}

1;

这(也许)比@ikegami 的代码更容易使用模块内部。

于 2012-08-20T12:22:10.600 回答