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??

Python_Dude
  • 507
  • 6
  • 12
  • 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) – sphere Dec 05 '13 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. – Joe Holloway Dec 05 '13 at 18:38
  • just want to call my thread function every 5 minus....It writes text file – Python_Dude Dec 05 '13 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. – Abhijit Dec 05 '13 at 18:40
  • 2
    [The `pass` statement](http://docs.python.org/2/tutorial/controlflow.html#pass-statements) does not do what you think it does. – bdesham Dec 05 '13 at 18:53
  • Does it causing problem??? – Python_Dude Dec 05 '13 at 18:54
  • The overall 'while True:' is never broken so it just runs forever – Farmer Joe Feb 26 '22 at 04:19

3 Answers3

3

You might rather use something like time.sleep

while True:
    # do something
    time.sleep(5*60) # wait 5 minutes
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
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()?

Community
  • 1
  • 1
Joe Holloway
  • 28,320
  • 15
  • 82
  • 92
  • Of course, you're grinding your CPU with repeated compare-and-branch – Leeor Dec 05 '13 at 18:56
  • 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 – Joe Holloway Dec 05 '13 at 18:57
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
ivcubr
  • 1,988
  • 9
  • 20
  • 28
Sonu
  • 11
  • 2