POE :: Kernel-> run()大约需要15秒才能停止/

时间:2014-07-11 14:28:55

标签: perl httpresponse poe

这是我的代码,已经编辑了“使用”语句。他们都按照需要工作。

my $json_data;

sub request {
    my ( $method, $container, $params, $content_type) = @_;

    #get the complete URL, works fine
    my $full_url = get_url( $method, $container, $params, $content_type);

    POE::Component::Client::HTTP->spawn(Alias => 'ua');

    # Start a session that will drive the resolver.
    # Callbacks are named functions in the "main" package.
    POE::Session->create(
    inline_states => {
        _start       => \&_start,
        got_response => \&got_response,
        }
    );

    POE::Kernel->run();
    return $json_data;
}

sub _start {
    my $kernel = $_[KERNEL];
    $kernel->post("ua" => "request", "got_response", $full_url);
}

sub got_response {
    my ($response_packet) = $_[ARG1];

    my $http_response = $response_packet->[0];
    my $response_string = $http_response->decoded_content();
    $json_data = decode_json( $response_string);
    print Dumper $json_data;
}

got_response 中的转储器会立即打印该值,但在此之后,我必须在 POE :: Kernel->运行之后等待至少15秒的返回语句执行。它返回正确的值,但我不能等那么久。 如果我在 sub get_reponse dumper 语句后添加exit,则不返回任何值。

任何帮助和建议都将不胜感激。

2 个答案:

答案 0 :(得分:2)

在每个会话结束之前,

run()将不会返回。这包括在run()运行时创建的会话。

在使用第一个POE模块之前定义常量POE::Kernel::TRACE_REFCNT,您将在整个程序的生命周期内收到一个资源正在使用的转储。

#!/usr/bin/perl

use strict;

sub POE::Kernel::TRACE_REFCNT () { 1 }
use POE;

# ...

$poe_kernel->run();
exit 0;

答案 1 :(得分:0)

列出了具体问题的更多细节。 POE::Component::Client::HTTP模块使用POE::Component::Client::Keepalive,后者又使用POE::Component::Resolver。 POE :: Component :: Resolver的默认超时为15秒。

立即关闭解析程序的最简单方法是在完成后关闭HTTP会话:

$kernel->post("ua" => "shutdown");
相关问题