Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Run code every 6seconde

by keszler (Priest)
on Dec 05, 2011 at 15:10 UTC ( #941874=note: print w/replies, xml ) Need Help??

in reply to Run code every 6seconde

Assuming that FONCTION1 and FONCTION2 will reliably execute in less than 6 seconds, and that exact timing is not critical:

my $did_this_time = 0; while (1) { my $now = time; if ( $now != $did_this_time && $now % 6 == 0 ) { $did_this_time = $now; FONCTION1(); FONCTION2(); } }

update: forgot to mention that this will keep the CPU rather busy... Adding a sleep 1; to the loop will help with that.

Replies are listed 'Best First'.
Re^2: Run code every 6seconde
by JavaFan (Canon) on Dec 05, 2011 at 15:56 UTC
    If it takes 3 seconds for FONCTION1() to run, and 4 seconds for FONCTION2(), your script waits 5 seconds for the next run. An alternative way would be:
    my $next = time; while (1) { $next = time if $next < time; $next += 6; FONCTION1; FONCTION2; sleep $next - time; }
    This still has all the caveats of using sleep (may sleep longer, may be terminated by a signal, may actually sleep for a second if the argument is 0), but it won't miss its "slot" if a run of FONCTION1(); FONCTION2() takes more than 6 seconds.
Re^2: Run code every 6seconde
by Anonymous Monk on Dec 05, 2011 at 15:43 UTC

    thanks for reply. after 60 second ,i need to close my file using FONCTION3.Can you correct me please or if you have another proposition.

    my $did_this_time = 0; my $i=0; Fonction0; while (1) { my $now = time; if ( $now != $did_this_time && $now % 6 == 0 ) { $did_this_time = $now; $i++; FONCTION1(); FONCTION2(); if (i==10) { FONCTION3(); break; } } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941874]
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-05-23 10:16 GMT
Find Nodes?
    Voting Booth?