Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:sourceforge': 0.03; 'subject:Python': 0.05; '(so': 0.07; 'subject:How': 0.09; 'python': 0.09; 'does,': 0.09; 'percentage': 0.09; 'received:mail- vc0-f174.google.com': 0.09; 'sep': 0.09; 'cc:addr:python-list': 0.10; 'url:)': 0.13; '(ubuntu': 0.16; 'idle,': 0.16; 'imposing': 0.16; 'jerry': 0.16; 'subject:limit': 0.16; 'subject:usage': 0.16; 'time).': 0.16; 'wrote:': 0.17; 'processor': 0.17; 'thu,': 0.17; 'solution.': 0.18; 'module': 0.19; 'all,': 0.21; 'combination': 0.22; 'cc:2**0': 0.23; 'apps': 0.23; 'tools,': 0.23; 'cc:no real name:2**0': 0.24; 'external': 0.24; 'linux': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'setting': 0.26; 'am,': 0.27; 'continuing': 0.27; '(e.g.,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'cpu': 0.29; 'priority': 0.29; 'received:209.85.220.174': 0.29; "i'm": 0.29; 'normally': 0.30; 'code': 0.31; 'url:python': 0.32; 'problem': 0.33; 'monitor': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'whatever': 0.35; 'received:209.85.220': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'useful': 0.36; 'possible': 0.37; 'level': 0.37; 'resources': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'url:docs': 0.38; 'instead': 0.39; 'easily': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'maximum': 0.63; 'more': 0.63; 'limit': 0.65; '20,': 0.65; 'saw': 0.75; 'run?': 0.84; 'typically,': 0.84; 'sitting': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=aN1p8OvyUW6URuNzztwCx/qHevtzzlNCZz76hW7/3uc=; b=ds+T0evm5YeRUbyxIe6po5AAU+lAwHQp2l6JGkOqctMsdBP7flpZmoH7xTWVC9MkAS FsYRIEkzeASezJqzx3DS/KGBBW7kWFLzOViBVFnsvVC5RSIwJkxdV7fZmJaaJrX1tyEp L5n698M8t0w203ERbv7bQEApj4UrqLSeq4g4zQuDmlOHAXtpPFCjRDOcg/gYH4N3Dtr3 DQ0qZ3DzXAxfjtR9Pb9cPuSCu2UB88mZMewp0nfQvoOAaPvN8QkRuVCTtNyHxnowV6XV 0TayHbFqyj4p9pEhj6YiMZf2zsTGWkGD6n9vSOQ4gkDE809S9NODibMpUksRezjzVjzW 2mvQ== MIME-Version: 1.0 In-Reply-To: <505B326C.1060503@cnic.edu.cu> References: <505B326C.1060503@cnic.edu.cu> Date: Thu, 20 Sep 2012 13:08:57 -0400 Subject: Re: How to limit CPU usage in Python From: Jerry Hill To: =?UTF-8?Q?Rolando_Ca=C3=B1er_Roblejo?= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348160940 news.xs4all.nl 6946 [2001:888:2000:d::a6]:43375 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29562 On Thu, Sep 20, 2012 at 11:12 AM, Rolando Ca=C3=B1er 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 f= or > 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, b= ut > 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. --=20 Jerry