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


Groups > comp.lang.python > #29562 > unrolled thread

Re: How to limit CPU usage in Python

Started byJerry Hill <malaclypse2@gmail.com>
First post2012-09-20 13:08 -0400
Last post2012-09-20 13:08 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#29562 — Re: How to limit CPU usage in Python

FromJerry Hill <malaclypse2@gmail.com>
Date2012-09-20 13:08 -0400
SubjectRe: How to limit CPU usage in Python
Message-ID<mailman.967.1348160940.27098.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web