Re: How to limit CPU usage in Python

Discussion in 'Python' started by Cameron Simpson, Sep 22, 2012.

  1. On 20Sep2012 12:53, Terry Reedy <> wrote:
    | On 9/20/2012 12:46 PM, Terry Reedy wrote:
    | > On 9/20/2012 11:12 AM, Rolando Cañer Roblejo wrote:
    | >> 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).
    | >
    | > Call the external tools with subprocess.open.
    |
    | I meant to end that with ? as I don't know how easy it is to get the
    | external id of the calling process that is to be limited. I presume that
    | can be done by first calling ps (with subprocess) and searching the
    | piped-back output.

    If you're limiting yourself, os.getpid().
    --
    Cameron Simpson <>
    Cameron Simpson, Sep 22, 2012
    #1
    1. Advertising

  2. you

    On Saturday, 22 September 2012 05:14:15 UTC+5:30, Cameron Simpson wrote:
    > On 20Sep2012 12:53, Terry Reedy <> wrote:
    >
    > | On 9/20/2012 12:46 PM, Terry Reedy wrote:
    >
    > | > On 9/20/2012 11:12 AM, Rolando Cañer Roblejo wrote:
    >
    > | >> 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).
    >
    > | >
    >
    > | > Call the external tools with subprocess.open.
    >
    > |
    >
    > | I meant to end that with ? as I don't know how easy it is to get the
    >
    > | external id of the calling process that is to be limited. I presume that
    >
    > | can be done by first calling ps (with subprocess) and searching the
    >
    > | piped-back output.
    >
    >
    >
    > If you're limiting yourself, os.getpid().
    >
    > --
    >
    > Cameron Simpson <>


    You could use os.times to compute the CPU usage and then stop the process when that happens and then start it after some time using signals.
    Ramchandra Apte, Sep 22, 2012
    #2
    1. Advertising

  3. you

    On Saturday, 22 September 2012 05:14:15 UTC+5:30, Cameron Simpson wrote:
    > On 20Sep2012 12:53, Terry Reedy <> wrote:
    >
    > | On 9/20/2012 12:46 PM, Terry Reedy wrote:
    >
    > | > On 9/20/2012 11:12 AM, Rolando Cañer Roblejo wrote:
    >
    > | >> 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).
    >
    > | >
    >
    > | > Call the external tools with subprocess.open.
    >
    > |
    >
    > | I meant to end that with ? as I don't know how easy it is to get the
    >
    > | external id of the calling process that is to be limited. I presume that
    >
    > | can be done by first calling ps (with subprocess) and searching the
    >
    > | piped-back output.
    >
    >
    >
    > If you're limiting yourself, os.getpid().
    >
    > --
    >
    > Cameron Simpson <>


    You could use os.times to compute the CPU usage and then stop the process when that happens and then start it after some time using signals.
    Ramchandra Apte, Sep 22, 2012
    #3
    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:
    155
    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:
    154
    Terry Reedy
    Sep 20, 2012
  3. Jerry Hill

    Re: How to limit CPU usage in Python

    Jerry Hill, Sep 20, 2012, in forum: Python
    Replies:
    0
    Views:
    171
    Jerry Hill
    Sep 20, 2012
  4. Paul Rubin

    Re: How to limit CPU usage in Python

    Paul Rubin, Sep 23, 2012, in forum: Python
    Replies:
    2
    Views:
    335
    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:
    183
    Dwight Hutto
    Sep 23, 2012
Loading...

Share This Page