Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Help with timeout

by kcott (Abbot)
on Sep 25, 2012 at 04:54 UTC ( #995486=note: print w/ replies, xml ) Need Help??


in reply to Help with timeout

G'day eversuhoshin,

"I have looked up Alarm but I don't know how to incorporate it into my code."

Using the same code structure given in the alarm example, here's how you might do this:

#!/usr/bin/env perl use strict; use warnings; my @iterations = map { 10 ** $_ } 0 .. 10; my $timeout = 2; for my $iterations_this_loop (@iterations) { eval { local $SIG{ALRM} = sub { die "TIMEOUT: $iterations_this_loop\n +" }; alarm $timeout; for (0 .. $iterations_this_loop) { # Processing here } alarm 0; }; if ($@) { die $@ unless $@ =~ /^TIMEOUT: \d+/; # propagate unexpected +errors print $@; next; } else { print "ENOUGH TIME: $iterations_this_loop\n"; } }

Output:

$ pm_long_for_alarm.pl ENOUGH TIME: 1 ENOUGH TIME: 10 ENOUGH TIME: 100 ENOUGH TIME: 1000 ENOUGH TIME: 10000 ENOUGH TIME: 100000 ENOUGH TIME: 1000000 ENOUGH TIME: 10000000 TIMEOUT: 100000000 TIMEOUT: 1000000000 TIMEOUT: 10000000000

for my $iterations_this_loop (@iterations) { represents the number of records in each file (that will be foreach (@files){ in your code).

for (0 .. $iterations_this_loop) { represents processing that number of records (that will be the processing you are doing within each iteration of your loop).

Note: the argument to alarm is in seconds - for 2 minutes you'll need my $timeout = 120;.

-- Ken


Comment on Re: Help with timeout
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995486]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2015-07-05 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls