2014-07-11 38 views
2

這是我的代碼,已經編輯了「use」語句。他們都按需要工作。POE :: Kernel-> run()需要大約15秒停止/

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 ::內核級>運行執行至少等待15秒return語句。它返回正確的值,但我不能等那麼久。 如果我在子get_reponse dumper語句後添加退出,則不會返回任何值。

任何幫助和建議,將不勝感激。

回答

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

感謝它幫助。我有3次會議。 –

+0

很高興幫助。乾杯! –