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


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

How to limit CPU usage in Python

Started byRolando Cañer Roblejo <rolando.caner@cnic.edu.cu>
First post2012-09-20 11:12 -0400
Last post2012-09-27 13:07 -0400
Articles 8 — 6 participants

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


Contents

  How to limit CPU usage in Python Rolando Cañer Roblejo <rolando.caner@cnic.edu.cu> - 2012-09-20 11:12 -0400
    Re: How to limit CPU usage in Python Tim Roberts <timr@probo.com> - 2012-09-22 13:55 -0700
      Re: How to limit CPU usage in Python Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-22 23:24 -0400
    Re: How to limit CPU usage in Python Paul Rubin <no.email@nospam.invalid> - 2012-09-22 16:05 -0700
      Re: How to limit CPU usage in Python Dwight Hutto <dwightdhutto@gmail.com> - 2012-09-22 23:04 -0400
      Re: How to limit CPU usage in Python Tim Roberts <timr@probo.com> - 2012-09-24 21:09 -0700
      RE: How to limit CPU usage in Python "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-27 16:58 +0000
      Re: How to limit CPU usage in Python Jerry Hill <malaclypse2@gmail.com> - 2012-09-27 13:07 -0400

#29554 — How to limit CPU usage in Python

FromRolando Cañer Roblejo <rolando.caner@cnic.edu.cu>
Date2012-09-20 11:12 -0400
SubjectHow to limit CPU usage in Python
Message-ID<mailman.960.1348154419.27098.python-list@python.org>
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).

Best regards.

[toc] | [next] | [standalone]


#29765

FromTim Roberts <timr@probo.com>
Date2012-09-22 13:55 -0700
Message-ID<q69s58dcr0u6nju4faddac6qqaglka5gi8@4ax.com>
In reply to#29554
Rolando Cañer Roblejo <rolando.caner@cnic.edu.cu> wrote:
>
>Is it possible for me to put a limit in the amount of processor usage (% 
>CPU) that my current python script is using?

Why?  That's an odd request.  It's natural to want to reduce your priority
if you want other processes handled first, but an idle CPU is a wasted
resource.  You want it to be busy all of the time.

>Some people recommend to use nice and cpulimit unix 
>tools, but those are external to python and I prefer a python solution. 

Scheduling and CPU priority are, by their very nature, operating system
concepts.  You will not find generic mechanisms wrapping them.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

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


#29779

FromDwight Hutto <dwightdhutto@gmail.com>
Date2012-09-22 23:24 -0400
Message-ID<mailman.1096.1348370698.27098.python-list@python.org>
In reply to#29765
Now also, just thinking theoretically with the knowledge I have,
you could underclock(as opposed to overclocking, which is what gamers
do), but have never seen that option in BIOS.

And maybe there is an option in your OS, google search term 'limiting
processes activity cpu usage':

https://www.google.com/search?client=ubuntu&channel=fs&q=limiting+processes+activity+cpu+usage&ie=utf-8&oe=utf-8

This seemed good for what you want from a brief overview:

http://www.cyberciti.biz/faq/cpu-usage-limiter-for-linux/


Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

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


#29769

FromPaul Rubin <no.email@nospam.invalid>
Date2012-09-22 16:05 -0700
Message-ID<7xpq5d7j5e.fsf@ruckus.brouhaha.com>
In reply to#29554
Rolando Cañer Roblejo <rolando.caner@cnic.edu.cu> writes:
> 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? 

One way is check your cpu usage once in a while, compare with elapsed
time, and if your % usage is above what you want, sleep for a suitable
interval before proceeding.

Tim Roberts: reasons to want to do this might involve a shared host
where excessive cpu usage affects other users; or a computer with
limited power consumption, where prolonged high cpu activity causes
thermal or other problems.

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


#29777

FromDwight Hutto <dwightdhutto@gmail.com>
Date2012-09-22 23:04 -0400
Message-ID<mailman.1095.1348369482.27098.python-list@python.org>
In reply to#29769
rites:
>> 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?
>
> One way is check your cpu usage once in a while, compare with elapsed
> time, and if your % usage is above what you want, sleep for a suitable
> interval before proceeding.
>

The script in constant runtime, unless it's in relation to other
processes, could be put on a % based sleep constant variable.

If the script is constantly running the same processes, and the OP
wants to limit it statistically, then at a critical portion in the
script sleep for a constant, or maybe, dynamic variable.

The only other is to create an app, disassemble it, and then refine
the asm instructions being used at the assembly level, but I'm just
scratching the surface of those enhancements.



-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

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


#30004

FromTim Roberts <timr@probo.com>
Date2012-09-24 21:09 -0700
Message-ID<0ib2689jpg2d11nlq09sfe1uv25mgbcbv0@4ax.com>
In reply to#29769
Paul Rubin <no.email@nospam.invalid> wrote:
>
>Tim Roberts: reasons to want to do this might involve a shared host
>where excessive cpu usage affects other users;

That's what priorities are for.

>...or a computer with
>limited power consumption, where prolonged high cpu activity causes
>thermal or other problems.

OK, I grant that.  However, statistically speaking, it is much more likely
that the OP merely has a misunderstanding.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

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


#30308

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-09-27 16:58 +0000
Message-ID<mailman.1498.1348765149.27098.python-list@python.org>
In reply to#29769
Paul Rubin wrote:
> Rolando Cañer Roblejo <rolando.caner@cnic.edu.cu> writes:
> > 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?
> 
> One way is check your cpu usage once in a while, compare with elapsed
> time, and if your % usage is above what you want, sleep for a suitable
> interval before proceeding.
> 
> Tim Roberts: reasons to want to do this might involve a shared host
> where excessive cpu usage affects other users; or a computer with
> limited power consumption, where prolonged high cpu activity causes
> thermal or other problems.

The problem is that checking the CPU usage is fairly misleading 
if you are worried about contention. If your process takes up 
100% of CPU and nothing else needs the resource, does it matter? 
I would not want to sleep *unless* something else needs the 
resource. Of course, there might be a good/easy way of checking
usage + contention, but I am unaware of any off the top of my
head.

On *nix you should just set the appropriate nice-ness and then 
let the OS handle CPU scheduling. Not sure what you would do 
for Windows--I assume OS X is the same as *nix for this context.



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

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


#30309

FromJerry Hill <malaclypse2@gmail.com>
Date2012-09-27 13:07 -0400
Message-ID<mailman.1499.1348765679.27098.python-list@python.org>
In reply to#29769
On Thu, Sep 27, 2012 at 12:58 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com> wrote:
> On *nix you should just set the appropriate nice-ness and then
> let the OS handle CPU scheduling. Not sure what you would do
> for Windows--I assume OS X is the same as *nix for this context.

On windows, you can also set the priority of a process, though it's a
little different from the *nix niceness level.  See
http://code.activestate.com/recipes/496767/ for a recipe using
pywin32.  I believe the psutil module handles this too, but I don't
think it manages to abstract away the platform differences.

-- 
Jerry

[toc] | [prev] | [standalone]


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


csiph-web