How to limit CPU usage in Python
Prasad, Ramit
ramit.prasad at jpmorgan.com
Thu Sep 27 12:58:45 EDT 2012
Paul Rubin wrote:
> Rolando Cañer Roblejo <rolando.caner at cnic.edu.cu> writes:
> > Is it possible for me to put a limit in the amount of processor usage
> > (% CPU) that my current python script is using? Is there any module
> > useful for this task?
>
> One way is check your cpu usage once in a while, compare with elapsed
> time, and if your % usage is above what you want, sleep for a suitable
> interval before proceeding.
>
> Tim Roberts: reasons to want to do this might involve a shared host
> where excessive cpu usage affects other users; or a computer with
> limited power consumption, where prolonged high cpu activity causes
> thermal or other problems.
The problem is that checking the CPU usage is fairly misleading
if you are worried about contention. If your process takes up
100% of CPU and nothing else needs the resource, does it matter?
I would not want to sleep *unless* something else needs the
resource. Of course, there might be a good/easy way of checking
usage + contention, but I am unaware of any off the top of my
head.
On *nix you should just set the appropriate nice-ness and then
let the OS handle CPU scheduling. Not sure what you would do
for Windows--I assume OS X is the same as *nix for this context.
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Python-list
mailing list