tags:

views:

201

answers:

5

How can I tell Perl to run some code every 20 seconds?

+6  A: 
while (1) {
        sleep 20;
        <your code here>;
}
MaQleod
What if "your code here" takes 19 seconds to run?
Ether
That depends. Does the code have to start on 20-second boundaries, or should there be 20 seconds between the last iteration finishing and the start of the next one?
Blrfl
+8  A: 
for (;;) {
    my $start = time;
    # your code;
    if ((my $remaining = 20 - (time - $start)) > 0) {
        sleep $remaining;
    }
}
eugene y
Upvote: does not suffer from drift.
daxim
@daxim It does suffer from drift if the `#your code` part takes longer than twenty seconds. To prevent drift it would need to be `sleep(($interval - (time - $start)) % $interval)` where `$interval` is twenty.
Chas. Owens
@daxim Of course, that suffers from a possible drift of just shy of a second per twenty.
Chas. Owens
+5  A: 

While the sleep function will work for some uses, if you're trying to do "every 20 seconds, forever", then you're better off using an external utility like cron.

In addition to the possible issue of drift already mentioned, if your sleep script exits (expectedly or otherwise), then it's not going to run again at the next 20 second mark.

@Blrfl is correct, and I feel sheepish. That said, it's easy enough to overcome.

* * * * * /path/to/script.pl
* * * * * sleep 20 && /path/to/script.pl
* * * * * sleep 40 && /path/to/script.pl

You could also take a hybrid approach of putting a limited count sleep loop in the script and using cron to run it every X minutes, covering the case of script death. Anything more frequent than 20 seconds, I would definitely take that approach.

RickF
POSIX `cron` doesn't do sub-minute resolution.
Blrfl
+1  A: 

Set up a SIGALRM handler, and send yourself a signal every 20 seconds with alarm (see perldoc -f alarm):

$SIG{ALRM} = sub {
    # set up the next signal for 20 seconds from now
    alarm 20;

    # do whatever needs to be done
    # ...
};

This will experience drift over time, as each signal may be delayed by up to a second; if this is important, set up a cron job instead. Additionally, even more drift will happen if your other code takes upwards of 20 seconds to run, as only one timer can be counting at once. You could get around this by spawning off threads, but at this point, I would have already gone back to a simple cron solution.

Choosing the proper solution is dependent on what sort of task you need to execute periodically, which you did not specify.

Ether
+1  A: 

See Schedule::ByClock:

#!/usr/bin/perl

use strict; use warnings;
use Schedule::ByClock;

my $scheduler = Schedule::ByClock->new(0, 20, 40);

while ( defined( my $r = $scheduler->get_control_on_second ) ) {
    printf "%02d\n", $r;
}

All the caveats others pointed out still apply, but I think the module is neat.

Sinan Ünür