Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:10:58+00:00 2026-06-08T17:10:58+00:00

Need run a subroutine every 5 seconds, but measured at system clock marks. So,

  • 0

Need run a subroutine every 5 seconds, but measured at system clock marks. So, need start it every minute at 0, 5, 10, 15…. 45, 50, 55 second (exactly, with 0.1sec precistion).

Something like:

for(;;) {
    do_sleep(); #time need to sleep to the next 5 second mark
    run_this();
}

The run_this sub can be fast or slow (its runtime between 0.2 – 120 seconds). When it run more than 5 seconds – regardless its runtime, the next run must be at exact 5 second mark.

E.g. when the run_this

  • ends at 11:11:12.3 need wait 2.7 second to the next run at 11:11:15
  • when ends at 11:11:59.2 need wait only 0.8 seconds to the next at 11:12:00, and so on…

The question is: how to write the do_sleep?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:10:58+00:00Added an answer on June 8, 2026 at 5:10 pm

    For 0.1s precision you need to use Time::HiRes module. Something like:

    #!/usr/bin/perl
    use 5.014;
    use warnings;
    use Time::HiRes qw(tv_interval usleep gettimeofday);
    
    for(;;) {
        do_sleep();
        run_this();
    }
    
    sub do_sleep {
        my $t = [gettimeofday];
        my $next = (int($t->[0]/5) + 1) * 5;
        my $delta = tv_interval ($t, [$next, 0]);
        usleep($delta * 1_000_000);
        return;
    }
    
    sub run_this {
        my $t = [gettimeofday];
        printf "Start is at: %s.%s\n",
            scalar localtime  $t->[0],
            $t->[1];
        usleep( rand 10_000_000 );  #simulating the runtime between 0-10 seconds (in microseconds)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to run application in every X seconds, so, as far as cron
I need to run a VBA subroutine 4 seconds later. The subroutine takes one
I need to run 'free -m' every minute and then add the result to
I need run a system call to run a .app file from java. Any
I need to run a java application (not an applet or JNLP, but a
This is my problem: I need run some code every time I open a
I need to run an if isset statement but i can't seem to get
I need to run a JavaScript function onLoad(), but only do it if the
i need run code that will create a database and populate tables. i am
I need run ts:reindex when smth add in model or destroy from model. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.