in reply to LWP::UserAgent timeout

1. LWP::UserAgent's default timeout is 180 seconds.

2. You set the timeout of your UserAgent AFTER you made the request. Obviously, it has no effect for the previous request ... (maybe use LWP::UserAgent::Pscychic for such cases? - just a joke :) )

3. Your GUI app is bound to freeze while conducting the server for long or short time. HTTP::Async can help you here, it will spawn a request in the background and when you are ready, you check if it has borne any fruits, like so (mostly borrowed from the manpage):

use strict; use warnings; use HTTP::Async; use HTTP::Request; my $url = ...; my $async = HTTP::Async->new; $async->add(HTTP::Request->new(GET => $url)); while ( $async->not_empty ) { if ( my $response = $async->next_response ) { # we have a reply from server or timeout # deal with $response } else { # still waiting for a response from server, # so do something else } }

bw, bliako

Replies are listed 'Best First'.
Re^2: LWP::UserAgent timeout
by markong (Pilgrim) on Dec 17, 2018 at 14:09 UTC

    I'd just add that, in either case, given that you are dealing with a GUI, it's usually a best practice to use a separate thread for the UI events processing and another for the processing:

    1. LWP::UserAgent::timeout() it's a lower bound: "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."
    2. Depending on what you do when you # deal with $response there are good chances you'll end freezing again your UI

      Multi-threads, i.e. separating UI and other stuff, make sure sense. However, it frightens me turning my application in a multi-threads one. My GUI is Tk and Tk is said to be "not thread safe". Zero experience on my side.

        Look at Tk::IO

        I will second the multi-thread model if you have heavy calculations or a fair amount of other networking. I was under the impression that your only network interaction would be the one you showed. However, I have zero knowledge of Tk myself.

Re^2: LWP::UserAgent timeout
by IB2017 (Pilgrim) on Dec 17, 2018 at 13:48 UTC

    Oh thanks. Anticipating the timeout declaration to:

    my $ua = LWP::UserAgent->new; $ua->timeout(0.1); my $req = HTTP::Request->new(GET => $url); my $res = $ua->request($req);

    makes it work. However, the idea to make it async is for sure a better idea. I'll give it a try.