Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Catching time-outs

by coldfingertips (Pilgrim)
on Mar 29, 2004 at 16:41 UTC ( #340667=perlquestion: print w/replies, xml ) Need Help??

coldfingertips has asked for the wisdom of the Perl Monks concerning the following question:

This is a revised repost of the last node I made a week ago. I am using LWP::Simple and UserAgent to parse links on a website, what I need to do is install a timeout feature for all these servers that take 2+ minutes to get ahold of. And on top of this, instead of timing out and skipping it without error, I NEED to print an error saying "Hey cowboy, this one timed out!" or something, as long as it doesn't just pretend it never existed.

My original post was refering to LWP::Simple, maybe I should have said I was using UserAgent? Does this make any difference?

I am parsing every link on a page (or trying to), but not if it takes more than 20 seconds to get a response from that server. Any ideas how to do a timeout like this?

Replies are listed 'Best First'.
Re: Catching time-outs
by davido (Cardinal) on Mar 29, 2004 at 16:49 UTC
    Have a look at the attributes of the new() method for LWP::UserAgent. You'll find that the final attribute is.......timeout.

    There is also the timeout() method found in the LWP::UserAgent module.

    The POD for the module states:

    $ua->timeout( $secs )

    Get/set the timeout value in seconds. The default timeout() value is 180 seconds, i.e. 3 minutes.

    The requests is aborted if no activity on the connection to the server is observed for timeout seconds. This means that the time it takes for the complete transaction and the request() method to actually return might be longer.

    I hope this helps!


      I've read that already and I know how to setup a custom timeout. My question is, how can I trap it so IF it times out MY error message will be displayed? It doesn't show you that.
        Did you also read the part of the docs that says the return value of the get() method is a response object, and to see HTTP::Response for details?

        In HTTP::Response, there is an is_success() and an is_error() method. And again we're directed somewhere else: see HTTP::Status for the meaning of these methods...

        RC_REQUEST_TIMEOUT is one of the possible response codes.


        If you die() or exit () in an eval block, the error goes to $! (or is it $@?) and the eval returns false. It's like Java's try/catch. If you die on not having success you can test for that. If, on the other hand, you mean that LWP::UserAgent is printing to STDERR, and you want to stop it, just redirect STDERR -- i.e. open ("STDERR", "> ./error").

        Want to support the EFF and FSF by buying cool stuff? Click here.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://340667]
Approved by davido
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-28 14:34 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (99 votes). Check out past polls.