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

Anyone have a good option for setting a timeout in GHTTP? I have an issue where my the VPN sometimes dies and the GHTTP call just hangs. The GHTTP documentation suggests looking at select, but it blocks on the $ghttp->process(). (And you need to run that before doing get_socket to get the socket handle for select.)

I really don't want to throw a SIG_ALARM, and forking a new process seems like overkill for just a simple timeout. Anyone have other options?

Replies are listed 'Best First'.
Re: GHTTP timeout
by pc88mxer (Vicar) on Apr 16, 2008 at 23:15 UTC
    From what I can tell, I'm not sure even using select will help you in all cases. For starters, the connect() call is not asynchronous. Additionally, the call to gethostbyname() can block, and this is probably what is happening when your VPN goes down.

    One possibility is to use a local proxy - then your process should never hang while establishing the HTTP connection.

Re: GHTTP timeout
by Khen1950fx (Canon) on Apr 17, 2008 at 04:49 UTC
    As an experiment, I tried this:

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use HTTP::GHTTP; my $r = HTTP::GHTTP->new('http://www.cpan.org'); $r->set_async; $r->set_chunksize(1024); $r->prepare; my $status; while ($status = $r->process) { select(undef, undef, undef, 0.1); } die "An error occured" unless defined $status; print $r->get_body;
    updated: Fixed code and added die...