CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
TIME OUT with a Perl Function

 



koundinya
New User

May 18, 2011, 6:26 PM

Post #1 of 2 (3725 views)
TIME OUT with a Perl Function Can't Post

I have a requirement in which a perl function gets me a set of values by connecting to multiple databases and merging the data. This data is then saved as a csv file and displayed on the browser screen.

Unforunately, the browser page is timing out ( 5 mins) before the execution of the script completes.

How can I identify if the function has retured any values within 4 mins (before the page times out) and if not , catch this exception and throw an error message on screen ( This report takes longer time for execution..!!! please wait ) and resume the session for another 4 minutes


FishMonger
Veteran / Moderator

May 19, 2011, 12:49 PM

Post #2 of 2 (3713 views)
Re: [koundinya] TIME OUT with a Perl Function [In reply to] Can't Post

C:\>perldoc -q timeout

Code
Found in C:\Perl\lib\pods\perlfaq8.pod 
How do I timeout a slow event?
Use the "alarm()" function, probably in conjunction with a signal
handler, as documented in "Signals" in perlipc and the section on
"Signals" in the Camel. You may instead use the more flexible
"Sys::AlarmCall" module available from CPAN.

The "alarm()" function is not implemented on all versions of Windows.
Check the documentation for your specific version of Perl.


C:\>perldoc -f alarm

Code
    alarm SECONDS 
alarm Arranges to have a SIGALRM delivered to this process after the
specified number of wallclock seconds has elapsed. If SECONDS is
not specified, the value stored in $_ is used. (On some
machines, unfortunately, the elapsed time may be up to one
second less or more than you specified because of how seconds
are counted, and process scheduling may delay the delivery of
the signal even further.)

Only one timer may be counting at once. Each call disables the
previous timer, and an argument of 0 may be supplied to cancel
the previous timer without starting a new one. The returned
value is the amount of time remaining on the previous timer.

For delays of finer granularity than one second, the Time::HiRes
module (from CPAN, and starting from Perl 5.8 part of the
standard distribution) provides ualarm(). You may also use
Perl's four-argument version of select() leaving the first three
arguments undefined, or you might be able to use the "syscall"
interface to access setitimer(2) if your system supports it. See
perlfaq8 for details.

It is usually a mistake to intermix "alarm" and "sleep" calls,
because "sleep" may be internally implemented on your system
with "alarm".

If you want to use "alarm" to time out a system call you need to
use an "eval"/"die" pair. You can't rely on the alarm causing
the system call to fail with $! set to "EINTR" because Perl sets
up signal handlers to restart system calls on some systems.
Using "eval"/"die" always works, modulo the caveats given in
"Signals" in perlipc.

eval {
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
alarm $timeout;
$nread = sysread SOCKET, $buffer, $size;
alarm 0;
};
if ($@) {
die unless $@ eq "alarm\n"; # propagate unexpected errors
# timed out
}
else {
# didn't
}

For more information see perlipc.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives