Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Just another Perl shrine
 
PerlMonks  

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.


Comment on Re: Run code every 6seconde
Select or Download Code
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; } } }
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://941874]
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: (14)
As of 2014-04-16 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls