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


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

Re: How to limit CPU usage in Python

Started byCameron Simpson <cs@zip.com.au>
First post2012-09-22 09:44 +1000
Last post2012-09-21 20:05 -0700
Articles 3 — 2 participants

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 Cameron Simpson <cs@zip.com.au> - 2012-09-22 09:44 +1000
    Re: How to limit CPU usage in Python Ramchandra Apte <maniandram01@gmail.com> - 2012-09-21 20:05 -0700
    Re: How to limit CPU usage in Python Ramchandra Apte <maniandram01@gmail.com> - 2012-09-21 20:05 -0700

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

FromCameron Simpson <cs@zip.com.au>
Date2012-09-22 09:44 +1000
SubjectRe: How to limit CPU usage in Python
Message-ID<mailman.1050.1348271054.27098.python-list@python.org>
On 20Sep2012 12:53, Terry Reedy <tjreedy@udel.edu> 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 <cs@zip.com.au>

[toc] | [next] | [standalone]


#29715

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-21 20:05 -0700
Message-ID<302aaed2-4317-43e6-8a3a-574049931844@googlegroups.com>
In reply to#29699
you

On Saturday, 22 September 2012 05:14:15 UTC+5:30, Cameron Simpson  wrote:
> On 20Sep2012 12:53, Terry Reedy <tjreedy@udel.edu> 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 <cs@zip.com.au>

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.

[toc] | [prev] | [next] | [standalone]


#29716

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-21 20:05 -0700
Message-ID<mailman.1058.1348283142.27098.python-list@python.org>
In reply to#29699
you

On Saturday, 22 September 2012 05:14:15 UTC+5:30, Cameron Simpson  wrote:
> On 20Sep2012 12:53, Terry Reedy <tjreedy@udel.edu> 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 <cs@zip.com.au>

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.

[toc] | [prev] | [standalone]


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


csiph-web