looping every x seconds

K

kernal32

Hello,

In PERL how do I execute a block of code every "x" seconds?
I'm talking a loop or similar but it runs every x seconds, where x is given.
 
K

kernal32

Hello,

In PERL how do I execute a block of code every "x" seconds?
I'm talking a loop or similar but it runs every x seconds, where x is
given.

Sorry I didnt think I was clear.
I'm using sleep 5 to wait 5 second before executing some code.

my question is, how can I keep the rest of the program running whilst the
loop continues in pararel
 
J

James Willmore

Sorry I didnt think I was clear.
I'm using sleep 5 to wait 5 second before executing some code.

my question is, how can I keep the rest of the program running
whilst the loop continues in pararel

Use fork. That's just one suggestion.

==untested==
#!/usr/bin/perl -w
use strict;

my $child = fork();
die "Can't fork: $!\n" unless defined $child;

if($child > 0){
#parent process
}else{
$child process
}


You'll need to add some signal handlers and maybe use some POSIX
fuctionality. Basically, 'salt to taste' ;)

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
If you think the United States has stood still, who built the
<largest shopping center in the world? -- Richard M. Nixon
 
R

Randal L. Schwartz

kernal32> I'm using sleep 5 to wait 5 second before executing some
kernal32> code.

kernal32> my question is, how can I keep the rest of the program
kernal32> running whilst the loop continues in pararel

A simple way is to insert a check into your main loop:

my $next_time = time + 5;
while (1) {
if (time > $next_time) {
... do your once every five seconds thing here ..
$next_time = time + 5;
}
... rest of code ...
}

If other things are event-driven, consider POE, found in the CPAN,
and described at http://poe.perl.org/.

print "Just another Perl hacker,"
 
J

Jürgen Exner

kernal32 said:
In PERL how do I execute a block of code every "x" seconds?
I'm talking a loop or similar but it runs every x seconds, where x is
given.

That is not possible from Perl.
You can put your the Perl program to rest for x seconds using sleep(). But
this is different from running the loop every x seconds.
Just imagine it takes y seconds to run the loop. Then the next start of the
loop will happen x+y seconds after the previous start.

If you really want/need to run the code every x seconds then you may need to
set up an external timer (depending on your needs for precision could be
another program or a system task or a hardware timer) which sends a signal
to your program and thus triggers the loop.

jue
 
A

Anno Siegel

Jürgen Exner said:
That is not possible from Perl.

Why on earth not?
You can put your the Perl program to rest for x seconds using sleep(). But
this is different from running the loop every x seconds.
Just imagine it takes y seconds to run the loop. Then the next start of the
loop will happen x+y seconds after the previous start.

What's stopping you from looking how long it took and subtracting that
from the sleep time? Or something equivalent, more robust? Within
the precision of the timer, it is quite possible to run a Perl loop
at specified intervals.

Anno
 
J

Joe

kernal32> I'm using sleep 5 to wait 5 second before executing some
kernal32> code.

kernal32> my question is, how can I keep the rest of the program
kernal32> running whilst the loop continues in pararel

A simple way is to insert a check into your main loop:

my $next_time = time + 5;
while (1) {
if (time > $next_time) {
... do your once every five seconds thing here ..
$next_time = time + 5;
}
... rest of code ...
}

If other things are event-driven, consider POE, found in the CPAN,
and described at http://poe.perl.org/.

print "Just another Perl hacker,"

2 cents...

Run "modulus"...


example.

###########################
use Time::Local; #Epoch Declares
my $stime = time();

my $often = 25; #number of secs

while (1)
{
if(!($stime % $often))
{
print "Hello.. I should print this every "$often\" secs\n";
}
$stime = time();
sleep 1;
}
exit;
###########################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top