Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: infinite loops == bad?

by Biker (Priest)
on Apr 24, 2002 at 08:12 UTC ( #161536=note: print w/ replies, xml ) Need Help??


in reply to infinite loops == bad?

I have a set of Perl programs that run in a controlled infinite loop. They are all supposed to work n times per minute and sleep inbetween. Furthermore, they are supposed to work at given moments in the minute, like for instance every time the seconds show 15 or 45.

As a way to control when these programs should execute or not, I use the CPAN module Schedule::ByClock, which will let me configure at what seconds in the minute the program should wake up and do things.

The Schedule::ByClock is a wrapper around sleep() which hides the somewhat bizarre calculation to find out how many seconds there are until the next time the seconds on the clock are for instance 12. Tricky if the seconds right now are at 57, since 12 - 57 != 15 and 15 is indeed the number of seconds to sleep() to reach the moment when the seconds are 12. (It's even tricky to explain in a clear way. ;-)

The module can also wait until given minutes within the hour, but I've never had reason to use that feature.


Everything went worng, just as foreseen.


Comment on Re: infinite loops == bad?
Select or Download Code
Re: Re: infinite loops == bad?
by blakem (Monsignor) on Apr 24, 2002 at 09:07 UTC
    $ perl -le '$now=57; $then=12; print +($then-$now)%60' 15

    -Blake

      Agreed, blakem.

      What I like using that module is that I can:

      my $th=Schedule::ByClock->new(12,33,55); # Some already defined values while(1) { $th->get_control_on_second(); work_hard(); if(condition()) { $th->get_control_on_second(42); # Special case. additional_work(); } }
      which gives a very clean main logic. I'm sure that it can be built in many ways (TMTOWTDI) but I really like this module. It's hiding the implementation details in a clean way.
      Everything went worng, just as foreseen.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-09-20 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls