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

  • Home
  • SEARCH
  • 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 8016917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:40:52+00:00 2026-06-04T20:40:52+00:00

What I want is to be able to run a function every second, irrelevant

  • 0

What I want is to be able to run a function every second, irrelevant of how long the function takes (it should always be under a second). I’ve considered a number of options but not sure which is best.

If I just use the delay function it isn’t going to take into account the time the function takes to run.

If I time the function and then subtract that from a second and make up the rest in the delay it’s not going to take into account the time calculations.

I tried using threading.timer (I’m not sure about the ins and outs of how this works) but it did seem to be slower than the 1s.

Here’s the code I tried for testing threading.timer:

def update(i):
    sys.stdout.write(str(i)+'\r')
    sys.stdout.flush()
    print i
    i += 1
    threading.Timer(1, update, [i]).start()

Is there a way to do this irrelevant of the length of the time the function takes?

  • 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-04T20:40:54+00:00Added an answer on June 4, 2026 at 8:40 pm

    This will do it, and its accuracy won’t drift with time.

    import time
    
    start_time = time.time()
    interval = 1
    for i in range(20):
        time.sleep(start_time + i*interval - time.time())
        f()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to run netstat -n and grab the output somehow
Is there any drawback to this setup? I want to be able to run
I'm using GNU Emacs on Win32. I want to be able to run jslint
I want a simple C method to be able to run hex bytecode on
I want to be able to launch a Run Script Build Phase in XCode
I need to run a function every time the application switches URLs in Backbone.js,
I'm using positioning.position(). but this function is blocking. I want to be able to
I want to be able to put PHP into the database and run it.
I want to be able to refresh the page every 2 minutes so that
I'm writing a micro MVC framework, and I want to be able to run

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.