16

I will be having a Python run 24/7 in a while loop, here is an example of the sort of program:

while True:

    print ("me again...")

But when I run it, my CPU goes to 100%! But I don't want that since my program will run for long lengths of time, etc. and I don't want my CPU getting very hot, is there anyway I can prevent this?

2
  • 3
    without wait the CPU goes 100% because that is what you told it to do :) print as fast as you possibly can. So wait will help offload that burden. print, generally is a very expensive operation. writing to a file is much cheaper.
    – Piotr Kula
    Jun 23, 2013 at 13:07
  • You need to tell the operating system that you do not want any cpu cycles for a given period of time, otherwise you cannot do anything to avoid what you see. Jun 26, 2013 at 18:17

4 Answers 4

18

At the end of your loop have a

time.sleep(xx) for seconds, or time.sleep(x.x) to represent partial seconds

(Please do remember to import the library time, like so: import time )

With xx being as high as possible without adversely effecting your program. Right now your program is always doing everything as fast as it can, rather than giving some time for the Pi to rest or do something else.

7
  • Hmmmm... Very interesting... I shall try it out. Thanks! Can't belive I didn't think of that!
    – user151324
    Jun 22, 2013 at 0:32
  • Is there a library I should import, etc. ? I am getting an error saying that time is not defined
    – user151324
    Jun 22, 2013 at 0:34
  • @coding_corgi yes, you need the time library.
    – Butters
    Jun 22, 2013 at 0:38
  • 2
    So import time?
    – user151324
    Jun 22, 2013 at 0:40
  • @coding_corgi. Yes, that is correct.
    – Butters
    Jun 22, 2013 at 0:43
16

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.

3

I had the same issue, see my question on Stack Exchange. The solution was a combination of time.sleep(0.01) and nice. nice lowers the CPU available to an application. This is how I start the app: nice -n 19.

1

You could also try nice -n 19 python myscript.py.

nice is a *nix utility to set the CPU priority of a task. 19 is the largest weight and consequently the slowest setting.

1
  • 2
    No. Raising the niceness of a process only helps by enabling other processes to get the CPUs more often. At any moment that there are no other processes, OP's code will still use 100 % CPU and if there are, they will use the CPU, totaling again at 100 %.
    – Bengt
    Jun 26, 2013 at 14:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.