Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29562

Re: How to limit CPU usage in Python

References <505B326C.1060503@cnic.edu.cu>
Date 2012-09-20 13:08 -0400
Subject Re: How to limit CPU usage in Python
From Jerry Hill <malaclypse2@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.967.1348160940.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Sep 20, 2012 at 11:12 AM, Rolando CaƱer Roblejo
<rolando.caner@cnic.edu.cu> wrote:
> Hi all,
>
> 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? I saw Resource module but I think it is not the module I am
> looking for. Some people recommend to use nice and cpulimit unix tools, but
> those are external to python and I prefer a python solution. I am working
> with Linux (Ubuntu 10.04).

Maximum percentage of CPU used isn't normally something you control.
The only way I know of to do it involves having another process
monitor the thing you want to control and sending signals to stop and
start it (e.g., http://cpulimit.sourceforge.net/).

Typically, you instead want to control the priority (so that higher
priority apps can easily take more CPU time).  That's what nice is for
(http://docs.python.org/library/os.html#os.nice).  If you want to
limit a process in the same way that ulimit does, then the resources
module is what you want
(http://docs.python.org/library/resource.html#resource.setrlimit).

Is there a particular reason that you'd rather have your CPU sitting
idle, rather than continuing with whatever code is waiting to be run?
I'm having a hard time understanding what problem you might be having
that some combination of setting the nice level and imposing resource
limits won't handle.

-- 
Jerry

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to limit CPU usage in Python Jerry Hill <malaclypse2@gmail.com> - 2012-09-20 13:08 -0400

csiph-web