Preface
Be sure you really need to run your task repeatedly. This is called busy waiting and almost always suboptimal. If your task is checking for the output of a subprocess, you can just subprocess.wait()
for it to finish, for example. If your task is to wait for a file or directory in the filesystem to be touched, you can use pyinotify to get your code triggered from the filesystem event handled by the kernel.
Answer
This is how you write infinite loop for busy waiting without consuming too much CPU.
Python 2:
from __future__ import print_function
from __future__ import division
import time
while True:
range(10000) # some payload code
print("Me again") # some console logging
time.sleep(0.2) # sane sleep time of 0.1 seconds
Python 3:
import time
while True:
range(10000) # some payload code
print("Me again") # some console logging
time.sleep(0.2) # sane sleep time of 0.1 seconds
Evaluation
As @gnibbler tested in another answer, the presented code should not consume more than 1 % CPU on recent machines. If it still consumes too much CPU with your payload code, consider raising the time to sleep even further. On the other hand, the payload code might need to get optimized for repeated execution. For example, Caching could speed up running on unchanged data.
Credits
This answer tries to builds upon @user2301728's answer.