Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'subject:How': 0.09; 'python': 0.09; 'compute': 0.09; 'terry': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; '(ubuntu': 0.16; 'limiting': 0.16; 'presume': 0.16; 'reedy': 0.16; 'simpson': 0.16; 'subject:limit': 0.16; 'subject:usage': 0.16; 'wrote:': 0.17; 'processor': 0.17; 'solution.': 0.18; 'module': 0.19; 'meant': 0.21; 'tools,': 0.23; 'cc:no real name:2**0': 0.24; 'external': 0.24; 'cc:2**1': 0.24; 'linux': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'cpu': 0.29; 'could': 0.32; '(with': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'useful': 0.36; 'possible': 0.37; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'end': 0.40; 'think': 0.40; 'easy': 0.60; 'first': 0.61; 'limited.': 0.64; 'limit': 0.65; 'saw': 0.75; 'yourself,': 0.75; 'received:209.85.216.184': 0.84 Newsgroups: comp.lang.python Date: Fri, 21 Sep 2012 20:05:39 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.167.84.54; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 122.167.84.54 MIME-Version: 1.0 Subject: Re: How to limit CPU usage in Python From: Ramchandra Apte To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, Terry Reedy X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348283142 news.xs4all.nl 6887 [2001:888:2000:d::a6]:46135 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29716 you On Saturday, 22 September 2012 05:14:15 UTC+5:30, Cameron Simpson wrote: > On 20Sep2012 12:53, Terry Reedy wrote: >=20 > | On 9/20/2012 12:46 PM, Terry Reedy wrote: >=20 > | > On 9/20/2012 11:12 AM, Rolando Ca=F1er Roblejo wrote: >=20 > | >> Is it possible for me to put a limit in the amount of processor usag= e (% >=20 > | >> CPU) that my current python script is using? Is there any module use= ful >=20 > | >> for this task? I saw Resource module but I think it is not the modul= e I >=20 > | >> am looking for. Some people recommend to use nice and cpulimit unix >=20 > | >> tools, but those are external to python and I prefer a python soluti= on. >=20 > | >> I am working with Linux (Ubuntu 10.04). >=20 > | > >=20 > | > Call the external tools with subprocess.open. >=20 > |=20 >=20 > | I meant to end that with ? as I don't know how easy it is to get the=20 >=20 > | external id of the calling process that is to be limited. I presume tha= t=20 >=20 > | can be done by first calling ps (with subprocess) and searching the=20 >=20 > | piped-back output. >=20 >=20 >=20 > If you're limiting yourself, os.getpid(). >=20 > --=20 >=20 > Cameron Simpson You could use os.times to compute the CPU usage and then stop the process w= hen that happens and then start it after some time using signals.