Ok, I've looked at the random bits of coded suggestions people have posted for handling http timeouts and unreachable servers. And am still a bit confused.

I have a script I've written to grab a bunch of urls from a server; however, every so often something chokes (I'm presuming on the server side) and my code just sits there trying to grab the page without ever returning. So, I was investigating the LWP::UserAgent timeout settings and it didn't seem to work (never times out) for this particular problem. I'm presuming that the server is basically giving some sort of error similar to the richard.com delima. But I don't know exactly and don't know how to figure it out.

So, I'm trying to hardcode a timeout so that it will try the url again after a certain amount of seconds since I'm pretty sure the server is available and should return a response.

The code below seems to work but looks kinda sloppy. Is there a better way to do all this? (I'm using LWP::Simple to keep things simple but could use whatever) Thanks in advance for your help!

$SIG{ALRM} = \&timed_out; $tryurl = 1; while ($tryurl) { eval { print "Getting url... $url\n"; alarm(10); $response = get $url; alarm(0); }; if ($@ =~ /BLAH/) { print "Timed out. Trying again...\n"; $tryurl = 1; } else { $tryurl = 0; } } print "Got url!\n"; sub timed_out { die "BLAH" }

In reply to LWP Timeout and Alarm handling by daviddhall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.