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

Right now, I'm making my program pause in the following manner:
$lastGood = time(); while( 1==1) { #print "."; #print time(), "\t", $lastGood, "\n"; if ((time() - $lastGood) > 300) #if it has been 5 minutes s +ince the last check, check again { #do something $lastGood = time(); } }
Now this process works just fine, but it eats up a lot of cpu cycles. I've thought about making it a cron job that is called every five minutes, but that would add some complexity, since the process has to know how often it failed when it is checking out some stats every five minutes. Is there a more efficient way to do this? Thanks!

Replies are listed 'Best First'.
Re: Making a program stop executing for five minutes
by davorg (Chancellor) on Feb 20, 2001 at 20:41 UTC
(redmist) Re: Making a program stop executing for five minutes
by redmist (Deacon) on Feb 20, 2001 at 22:21 UTC

    Or you could use select():
    select(undef, undef, undef, 298.6);    # Be precise That way, your program will sleep for exactly 298.6 seconds. sleep() will actually sleep more or less than you want depending on what architecture you are on.

    redmist
    Silicon Cowboy
Re: Making a program stop executing for five minutes
by dws (Chancellor) on Feb 20, 2001 at 22:27 UTC
    This may be a fundamental misunderstanding worth expanding on.

    To most programmers, "pause" means to relinquish the CPU for some amount of time. This is typically done by a sleep() call, which lets your program relinquish the CPU for multiples of 1 second, or by a select() call, which relinquishes the CPU for multiples of a millisecond.

    By "relinquish" we mean "not run". The operating system won't schedule your program for execution until at least the specified period of time has elapsed. None of your program's instructions are executed. The CPU is then available for other processes.

    Rather than relinquishing the processor, the script above runs in place, in a fairly tight loop, until it sees that the clock has reached the desired time. As you've noted, this isn't processor friendly, and probably isn't what you intended.

Re: Making a program stop executing for five minutes
by zigster (Hermit) on Feb 20, 2001 at 20:43 UTC
    Shudder You are right to be wary the method you have selected is not optimal for CPU load *grins* look at perlfunc:sleep will allow you to sleep for some seconds.
    --

    Zigster
Re: Making a program stop executing for five minutes
by TheoPetersen (Priest) on Feb 20, 2001 at 20:42 UTC
Re: Making a program stop executing for five minutes
by McD (Chaplain) on Feb 21, 2001 at 00:23 UTC
    RichardH writes:

    I've thought about making it a cron job that is called every five minutes, but that would add some complexity, since the process has to know how often it failed when it is checking out some stats every five minutes.

    Everyone was quick (and correct) to point out the merits of sleep(), but to answer this part of your question - persist your data out to a file after you run, and persist it back in when you get restarted from cron.

    Depending on the details of what you're trying to do, this might be the better soloution.

    For anything beyond very simple persistance, may I heartily reccomend getting cozy with:

    use Data::Dumper;

    which has been part of the standard Perl distribution for some time. Great stuff.

    Peace,
    -McD

Re: Making a program stop executing for five minutes
by ChOas (Curate) on Feb 20, 2001 at 20:42 UTC
    Hi!

    I might totally miss the question, but how about sleep(300); ?

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;