3

I am using this loop for running every 5 minutes just creating thread and it completes.

while True:
        now_plus_5 = now + datetime.timedelta(minutes = 5)
        while datetime.datetime.now()<= now_plus_5:
                new=datetime.datetime.now()
                pass
        now = new
        pass

But when i check my process status it shows 100% usage when the script runs.Does it causing problem?? or any good ways??

Does it causes CPU 100% usage??

7
  • sure, you leave the inner while-loop after 5 minutes but the condition check get executed as fast as possible. You should use time.sleep(300) Commented Dec 5, 2013 at 18:37
  • What are you trying to do? Your while loops here are CPU bound. There's most likely a better construct that you can employ. Commented Dec 5, 2013 at 18:38
  • just want to call my thread function every 5 minus....It writes text file Commented Dec 5, 2013 at 18:40
  • 2
    Why is this question getting down-votes? I see a perfect question from someone new to python and programming. I have seen lot of novice programmers or programmers who came from real mode programming era thinks this to be a valid way to delay a processing. Commented Dec 5, 2013 at 18:40
  • 2
    The pass statement does not do what you think it does. Commented Dec 5, 2013 at 18:53

3 Answers 3

3

You might rather use something like time.sleep

while True:
    # do something
    time.sleep(5*60) # wait 5 minutes
Sign up to request clarification or add additional context in comments.

Comments

3

Based on your comment above, you may find a Timer object from the threading module to better suit your needs:

from threading import Timer

def hello():
    print "hello, world"

t = Timer(300.0, hello)
t.start() # after 5 minutes, "hello, world" will be printed

(code snippet modified from docs)

A Timer is a thread subclass, so you can further encapsulate your logic as needed.

This allows the threading subsystem to schedule the execution of your task such that it's not entirely CPU bound like your current implementation.

I should also note that the Timer class is designed to be fired only once. As such, you'd want to design your task to start a new instance upon completion, or create your own Thread subclass with its own smarts.

While researching this, I noticed that there's also a sched module that provides this functionality as well, but rather than rehash the solution, check out this related question:

Python Equivalent of setInterval()?

2 Comments

Of course, you're grinding your CPU with repeated compare-and-branch
Yes, your program as written is not doing any I/O so the while loops are pegging the CPU. You can hack it by using time.sleep to give the CPU a breather on each iteration, but the Timer class already encapsulates what you're trying to do
1

timedelta takes(seconds,minutes,hours,days,months,years) as input and works accordingly

from datetime import datetime,timedelta
end_time = datetime.now()+timedelta(minutes=5)
while end_time>= datetime.now():
    statements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.