1

I am developing some Python code for Windows. A criteria is that it will use less than 1% of CPU. I understand that it is impossible to guarantee this all the time due to things like garbage collection, but what would be the best practice to get as close as possible. My current solution is to spread a lot of time.sleep(0.1) around the code, especially in loops. There are, however, obvious problems with this approach.

What other approaches could be taken?

I should also mention that the application has lots of threads in it using the threading library.

EDIT: Setting the process priority is not what I am after.

8
  • Try telling us all some more about why you need to keep the CPU load so low despite having code that's not I/O-bound. (I/O-bound code – including a lot of GUI and network code – naturally ends up with low CPU usage.) Commented Jun 23, 2011 at 9:15
  • 3
    Something feels off in what you're trying to do. It might be better to be using lots of CPU, but only for short amounts of time so you can get back to waiting for something to happen for real. Commented Jun 23, 2011 at 9:25
  • 3
    Right, what you should do it run it with a low processor priority (and maybe also make sure you call the right system calls so you can deal with the system events and such quickly.) But this may be a problem if your customer doesn't understand these things. Sleep() doesn't seem to be the right solution though, rather scheduling the processor intensive things. Can you say more about he task? Commented Jun 23, 2011 at 9:29
  • 4
    @I_like_traffic_lights: Please do not add comments to a question you own. Please update the question to be complete and delete the long thread of hard-to-read and confusing comments. Commented Jun 23, 2011 at 9:58
  • 2
    @I_like_traffic_lights: "it is impossible for an application to determine whether the user is waiting for the CPU to process something" - That what priority is for. By setting your process to low priority, the operating system will always let the users process go first. Your software don't have to figure this out, it is the responsibility of the operating system. Read up on CPU scheduling and priority. Commented Jun 23, 2011 at 11:34

3 Answers 3

11

It is the job of the operating system to schedule CPU time. Use your operating system's built-in process-limits mechanisms (hopefully they exist on Windows) to restrict your process to <1% CPU.

This style of sprinkling unnecessary sleeps every few lines in the code will make the code terrible to create and extend and maintain, not to mention incredibly inelegant. (Rate-limiting yourself may be useful in very small, limited, critical sections -- for example your program is queuing lots of IO requests and you don't wish to inundate the operating system, you might wish to put a single sleep-until-[condition] in each critical loop which has the potential to inundate the system, but otherwise use extremely sparingly.)

Ideally you would call an API to the appropriate OS mechanisms from within your program when you start up, telling the OS to throttle you appropriately.

Sign up to request clarification or add additional context in comments.

1 Comment

I do not believe such a function exists under Windows.
11

If the goal is to not bother the user then "below 1% CPU" is the wrong approach. What you really want is "don't take time away from other processes but still complete as fast as possible" - that's what "below normal" process priority is for. See http://code.activestate.com/recipes/496767-set-process-priority-in-windows/ for an example of how process priority can be changed for the current process (calling that function with default parameters will do).

For the sales pitch you can show the task manager while the computer is idle ("See? 99%, my application gets lots of work done") and then start some CPU-intensive application ("Almost all CPU time is spent in the application the user is working with, my application simply went into background").

1 Comment

The problem is that I might not be there when the guys test the application. I might just hear, that my application was uninstalled, because it was found to be the reason for some recent complaints about the performance of the systems (it is easy to use such an application as a scapegoat).
3

If the box used for the demonstration is a Windows Server, it can use Windows System Resource Manager for restricting CPU usage below the desired threshold. Trying to force this behavior by code is impossible, unless a Windows API exposes this capability explicitly.

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.