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 8237229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:24:19+00:00 2026-06-07T19:24:19+00:00

I have a threaded class whose loop needs to execute 4 times every second.

  • 0

I have a threaded class whose loop needs to execute 4 times every second. I know that I can do something like

do_stuff()
time.sleep(0.25)

but the problem is that is doesn’t account for the time it takes to do_stuff(). Effectively this needs to be a real-time thread. Is there a way to accomplish this? Ideally the thread would still be put to sleep when not executing code.

  • 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-07T19:24:21+00:00Added an answer on June 7, 2026 at 7:24 pm

    The simple solution

    import threading
    
    def work (): 
      threading.Timer(0.25, work).start ()
      print "stackoverflow"
    
    work ()
    

    The above will make sure that work is run with an interval of four times per second, the theory behind this is that it will "queue" a call to itself that will be run 0.25 seconds into the future, without hanging around waiting for that to happen.

    Because of this it can do it’s work (almost) entirely uninterrupted, and we are extremely close to executing the function exactly 4 times per second.


    More about threading.Timer can be read by following the below link to the python documentation:

    • docs.python.org – 16.2.7. Timer Objects

    RECOMMENDED] The more advanced/dynamic solution

    Even though the previous function works as expected you could create a helper function to aid in dealing with future timed events.

    Something as the below will be sufficient for this example, hopefully the code will speak for itself – it is not as advanced as it might appear.

    See this as an inspiration when you might implement your own wrapper to fit your exact needs.

    import threading
    
    def do_every (interval, worker_func, iterations = 0):
      if iterations != 1:
        threading.Timer (
          interval,
          do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
        ).start ()
    
      worker_func ()
    
    def print_hw ():
      print "hello world"
    
    def print_so ():
      print "stackoverflow"
    
    
    # call print_so every second, 5 times total
    do_every (1, print_so, 5)
    
    # call print_hw two times per second, forever
    do_every (0.5, print_hw)
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating some multi-threaded code, and I have created a JobDispatcher class that
I have a class which contains a static field that acts like a singleton
I have a helper class that creates some objects, like a builder. The helper
I have a XAML form that I would like two independent features threaded out
I tried to write a program that can have mutiple-threaded reads and writes. It
Possible Duplicate: pthread Function from a Class I have this code that I can't
I have a threaded server application that stores List<Item> dataList . There is a
I have a single-threaded application that uses 3 SQLite databases in 3 different files
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
I have a pyinotify watcher running threaded, called as a separate class, at the

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.