1

サーバーを数秒間停止してから再起動したい。を使おうとしていますsleep(5)。これは役に立ちますか?

以下を含むPerlスクリプトで試しました:

if($mech=~ m/(Connection refused)/) {print "SLEEPING\n";sleep(5);redo;}
4

1 に答える 1

0

サーバーは Web ページをホストします。クライアントがサーバーに接続します。WWW::Mechanize を使用する Perl スクリプトはクライアントです。

次のことをしようとしていると思われます。

my $retries = 3;
my $resp; # response goes here
while ( $retries-- > 0 ) {
    $resp = $ua->get( "http://www.google.com/" );
    if ( ! $resp->is_success ) {
        warn( "Failed to get webpage: " . $resp->status_line );
        sleep( 5 );
        next; # continue the while loop
    }

    last; # success!
}

if ( $retries == 0 ) {
    die( "Too many retries!" );
}

# old versions of LWP::UserAgent didn't have decoded_content()
my $content = $resp->can('decoded_content') ? 
    $resp->decoded_content : $resp->content;

print( $content );

アップデート

コメントで提供したコードは次のとおりです。

use WWW::Mechanize;
my $mech = new WWW::Mechanize;
eval {
    $mech->get($url);
}; 
if($@) { 
    print STDERR "Burst\n";
    print STDERR Dumper($mech);
    # my $var=$mech;
    # print STDERR "var $var\n";
    # if($mech=~ m/(Connection refused)/) 
    # {
    #     print "SLEEPING\n";
    #     sleep(5);
    #     redo;
    # }

からperdoc -f redo:

「やり直し」コマンドは、条件を再度評価せずにループ ブロックを再開します。「継続」ブロックは、もしあれば実行されません。LABEL を省略すると、コマンドは最も内側のループを参照します。

コードをループに入れていないため、呼び出しredoは何の効果もありません。

于 2013-09-25T15:41:50.940 に答える