Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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.

Replies are listed 'Best First'.
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


      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://161536]
[Corion]: A good morning to everybody ;)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-06-24 06:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.