On Tue, Apr 7, 2020 at 6:02 AM ToddAndMargo via perl6-users <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> wrote:

    Hi All,

    Windows 7/10

    Another piece of the puzzle.

    I want to loop Raku program once an hour.

    Is it better use `sleep 3600` or let the program
    die and restart every hour from the Task Scheduler.
    By better, I mean less of a CPU footprint.

    `sleep` would allow the user to cancel the program
    and not have it come back.

    Many thanks,
    -T


On 2020-04-07 19:22, Brad Gilbert wrote:
Run code once an hour:

     react whenever Supply.interval(60 * 60) {
         say "it's been an hour"
     }

Right now that gets about 0.01 seconds slower every time the interval runs.
(At least on my computer.)
So it will get 1 second later every 4 days.


Or if you want more precise control, you could do something like:

Here is an example that is more accurate, and happens at the top of the hour:

     sub every-hour ( --> Supply:D ) {
         supply {
             sub add-next ( $hour --> Nil ) {
                 whenever Promise.at( $hour.Instant ) {
                     emit $hour;
                     add-next( $hour.later(:1hour) );
                 }
             }

             add-next( DateTime.now.truncated-to('hour').later( :1hour ) );
         }
     }


     react whenever every-hour() {
         say "it's been an hour";
         say "the time is $_"
     }



Honestly it would probably be better to use something native to the system to run it once an hour, in case the program dies.



Thank you!

How much impact CPU usage wise does sleep and/or react
have on the system?

My fear is I will tie up a core with my code

Reply via email to