Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Running code a certain amount of time

by Ninth Prince (Acolyte)
on Oct 31, 2008 at 14:57 UTC ( [id://720710]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I would like to add a condition to a while loop that exits the loop if the code has been executing for certain amount of time. For example I would like the while to exit if ($count>100 or time>60 minutes). Is there a standard way for doing this? I don't need anything fancy. I just want to be able to set the program to run for a couple of hours (if need be) and then stop.

Thanks.

Replies are listed 'Best First'.
Re: Running code a certain amount of time
by ccn (Vicar) on Oct 31, 2008 at 15:02 UTC

    use alarm function and catch SIG{ALRM} signal

    sub some_sub { eval { local $SIG{ALRM} = sub {die "died in SIG ALRM";}; alarm(1); my $i=0; $i++ while 1; alarm(0); }; if ($@) { if ($@ =~ /died in SIG ALRM/) { print "caught\n"; } else { print $@; } } }
Re: Running code a certain amount of time
by Limbic~Region (Chancellor) on Oct 31, 2008 at 17:50 UTC
    Ninth Prince,
    The example by ccn allows you to stop execution at almost any arbitrary point. What you haven't explained is what is going on inside the loop. Signals changed in 5.8 IIRC, so knowing if you want safe or unsafe signals is only something you can answer. There is an alternative if you want the code to exit the loop before starting the next loop (not anywhere in the middle of the loop).
    my $limit = time + 3_600; my $cnt; while (1) { ++$cnt; last if $cnt > 100 || time > $limit; # rest of your loop }

    Cheers - L~R

Re: Running code a certain amount of time
by blazar (Canon) on Nov 01, 2008 at 09:52 UTC
    I just want to be able to set the program to run for a couple of hours (if need be) and then stop.

    (additional emphasis by me)

    I personally believe that for such prolonged times a solution along the lines of that suggested by L~R is best suited. Playing with alarm as duly suggested by ccn is more important in short lived situations in which finer control over time is needed and/or you need to timeout "no matter what" e.g. when running external processes:

    Incidentally, it is worth reminding that for finer control over (not necessarily) short times a precious module would be Time::HiRes, which is in core.

    --
    If you can't understand the incipit, then please check the IPB Campaign.
Re: Running code a certain amount of time
by zentara (Archbishop) on Nov 01, 2008 at 13:16 UTC
Re: Running code a certain amount of time
by mpeever (Friar) on Oct 31, 2008 at 16:34 UTC

    If you're just trying to limit the number of iterations in your loop, something this simple will work:

    my $iterations = 60; while ( something_is_true and $iterations-- ) { do_something(); }

Re: Running code a certain amount of time
by afresh1 (Hermit) on Oct 31, 2008 at 18:57 UTC

    Deleted everything, since I was just agreeing with L~R. I swear I loaded the tab in the background before he posted and forgot to reload before posting. :-)

    l8rZ,
    --
    andrew

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found