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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:36:12+00:00 2026-05-30T01:36:12+00:00

I am trying to run python application and execute actions based on specified interval.

  • 0

I am trying to run python application and execute actions based on specified interval. Below code is consuming constantly 100% of CPU.

def action_print():

    print "hello there"

interval = 5
next_run = 0

while True:

    while next_run > time.time():
        pass

    next_run = time.time() + interval

    action_print()

I would like to avoid putting process to sleep as there will be more actions to execute at various intervals.

please advise

  • 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-05-30T01:36:14+00:00Added an answer on May 30, 2026 at 1:36 am

    If you know when the next run will be, you can simply use time.sleep:

    import time
    interval = 5
    next_run = 0
    while True:
       time.sleep(max(0, next_run - time.time()))
    
       next_run = time.time() + interval
       action_print()
    

    If you want other threads to be able to interrupt you, use an event like this:

    import time,threading
    interval = 5
    next_run = 0
    interruptEvent = threading.Event()
    while True:
       interruptEvent.wait(max(0, next_run - time.time()))
       interruptEvent.clear()
    
       next_run = time.time() + interval
       action_print()
    

    Another thread can now call interruptEvent.set() to wake up yours.

    In many cases, you will also want to use a Lock to avoid race conditions on shared data. Make sure to clear the event while you hold the lock.

    You should also be aware that under cpython, only one thread can execute Python code. Therefore, if your program is CPU-bound over multiple threads and you’re using cpython or pypy, you should substitute threading with multiprocessing.

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

Sidebar

Related Questions

I am trying to run a simple multiple processes application in Python. The main
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually
I'm trying to use Python to run pdftotext, but for some reason, my code
I'm trying to run Python scripts using Xcode's User Scripts menu. The issue I'm
I am using Python 2.6 and am trying to run a simple random number
I'm trying to write a program in Python and I'm told to run an
I'm new to python but I've run into a hitch when trying to implement
I'm trying to schedule a repeating event to run every minute in Python 3.
I am trying to package a python application on my 64 bit windows 7
I am trying to write a cross-platform python program that would run in 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.