Re: How to limit CPU usage in Python

Discussion in 'Python' started by Jerry Hill, Sep 20, 2012.

  1. Jerry Hill

    Jerry Hill Guest

    On Thu, Sep 20, 2012 at 11:12 AM, Rolando Cañer Roblejo
    <> 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
    Jerry Hill, Sep 20, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Reedy

    Re: How to limit CPU usage in Python

    Terry Reedy, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    160
    Terry Reedy
    Sep 20, 2012
  2. Terry Reedy

    Re: How to limit CPU usage in Python

    Terry Reedy, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    161
    Terry Reedy
    Sep 20, 2012
  3. Cameron Simpson

    Re: How to limit CPU usage in Python

    Cameron Simpson, Sep 22, 2012, in forum: Python
    Replies:
    2
    Views:
    276
    Ramchandra Apte
    Sep 22, 2012
  4. Paul Rubin

    Re: How to limit CPU usage in Python

    Paul Rubin, Sep 23, 2012, in forum: Python
    Replies:
    2
    Views:
    341
    Jerry Hill
    Sep 27, 2012
  5. Dwight Hutto

    Re: How to limit CPU usage in Python

    Dwight Hutto, Sep 23, 2012, in forum: Python
    Replies:
    0
    Views:
    187
    Dwight Hutto
    Sep 23, 2012
Loading...

Share This Page