【问题标题】:POE::Kernel->run() takes around 15 seconds to stop/POE::Kernel->run() 大约需要 15 秒才能停止/
【发布时间】:2014-07-11 14:28:54
【问题描述】:

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

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 中的 Dumper 会立即打印该值,但之后我必须等待至少 15 秒才能在 POE::Kernel->run 之后等待返回语句执行。它返回正确的值,但我不能等那么久。 如果我在 sub get_reponse dumper 语句之后添加 exit,则不会返回任何值。

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

【问题讨论】:

    标签: perl httpresponse poe


    【解决方案1】:

    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;
    

    【讨论】:

    • 感谢它的帮助。我有 3 节课。
    • 很高兴为您提供帮助。干杯!
    【解决方案2】:

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

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

    $kernel->post("ua" => "shutdown");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2023-03-20
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多