control CPU usage

Dave Angel davea at ieee.org
Sat Sep 19 17:22:47 EDT 2009


kakarukeys wrote:
> Hi,
>
> When I am running a loop for a long time, calculating heavily, the CPU
> usage
> is at 100%, making the comp not so responsive. Is there a way to
> control the
> CPU usage at say 80%? putting a time.sleep(0.x) doesn't seem to help
> although CPU usage level is reduced, but it's unstable.
>
> Regards,
> W.J.F.
>
>   
Controlling a task's scheduling is most definitely OS-dependent., so you 
need to say what OS you're running on.  And whether it's a multi-core 
and or duo processor.

In Windows, there is a generic way to tell the system that you want to 
give a boost to whatever task has the user focus (generally the 
top-window on the desktop).  On some versions, that's the default, on 
others, it's not.  You change it from Control Panel.  I'd have to go 
look to tell you what applet, but I don't even know if you're on Windows.

In addition, a program can adjust its own priority, much the way the 
Unix 'nice' command works.  You'd use the Win32 library for that.

And as you already tried, you can add sleep() operations to your 
application.

But if you're looking at the task list in the Windows Task Manager, you 
aren't necessarily going to see what you apparently want.  There's no 
way to programmatically tell the system to use a certain percentage for 
a given task.  If there's nothing else to do, then a low priority task 
is still going to get nearly 100% of the CPU.  Good thing.  But even if 
there are other things to do, the scheduling is a complex interaction 
between what kinds of work the various processes have been doing lately, 
how much memory load they have, and what priority they're assigned.

If you just want other processes to be "responsive" when they've got the 
focus, you may want to make that global setting.  But you may need to 
better define "responsive" and "unstable."

DaveA



More information about the Python-list mailing list