Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'causing': 0.04; 'none:': 0.07; 'exception,': 0.09; 'immutable': 0.09; 'subject:script': 0.09; 'try:': 0.09; 'django': 0.11; 'long"': 0.16; 'self.cmd': 0.16; 'sys.exit(1)': 0.16; 'true:': 0.16; 'prevent': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'machine': 0.22; 'memory': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'this?': 0.23; 'error': 0.23; 'script': 0.25; 'query': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:( 40': 0.30; 'message-id:@mail.gmail.com': 0.30; 'away.': 0.31; 'larry': 0.31; 'anyone': 0.31; 'this.': 0.32; 'figure': 0.32; 'subject:all': 0.32; 'running': 0.33; 'skip:d 20': 0.34; 'except': 0.35; 'skip:s 30': 0.35; 'tool': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'doing': 0.36; 'too': 0.37; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:t 30': 0.61; 'name': 0.63; 'hours': 0.66; 'here': 0.66; 'mar': 0.68; 'forks': 0.84 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 :content-type; bh=iJkk2IEFd7iVkDy4dEqVyD4mjONI+4JEChQCNDt13LQ=; b=d29mbOllQxvts7DFjoZZbDPcPeSdsWN7bZG0P/wSQj7HFWMWddsLigWvTi5+CyQecj eMl3+imvI+/CKvy7VD8ZEr0hWCy+I7eemqHJUaRxn5n++IElgf4P7COHN23QvUxAJ0rS T7f3ZwBgGIElHBre2q5xIVGdgIkeH+JcGkbjdGkEUN86AOMvkzYDWrKmKj/5SeRX+oSF Y5dxiOLd9w4MDF5SB1Chswgluu3grbfk9u/Tkk5o1SxA/O2458Zn2RH+IGos/uYW4bQg 9yos31ZZmk7obWBW1U/yhRWPY1MfSkYZY2+5qC5BBfAMCP+VuBUEMIsvv/A69L2FBxlu mipA== MIME-Version: 1.0 X-Received: by 10.194.82.105 with SMTP id h9mr13832221wjy.52.1394143002898; Thu, 06 Mar 2014 13:56:42 -0800 (PST) In-Reply-To: References: Date: Thu, 6 Mar 2014 16:56:42 -0500 Subject: Re: script uses up all memory From: Larry Martell To: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394143009 news.xs4all.nl 2872 [2001:888:2000:d::a6]:41933 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67953 On Wed, Mar 5, 2014 at 5:27 PM, Larry Martell wrote: > I have a script that forks off other processes and attempts to manage > them. Here is a stripped down version of the script: > > self.sleepTime = 300 > self.procs = {} > self.startTimes = {} > self.cmd = ['python', '/usr/local/motor/motor/app/some_other_script.py'] > > while True: > try: > self.tools = Tool.objects.filter(ip__isnull=False) > except Exception, e: > print 'error from django call: ' + str(e) > sys.exit(1) > > for tool in self.tools: > name = tool.name > if name in self.procs: > if self.procs[name].poll() is None: > if > (datetime.datetime.now()-self.startTimes[name]) > > datetime.timedelta(hours=12): > # it's been running too long - kill it > print 'killing script for ' + name + " > it's been running too long" > self.procs[name].kill() > else: > continue > > if self.procs[name].returncode: > print 'scrikpt failed for ' + name + ', error > = ' + str(self.procs[name].returncode) > > print 'starting script.py for ' + name + ' at ' + > str(datetime.datetime.now()) > try: > self.procs[name] = subprocess.Popen(self.cmd) > self.startTimes[name] = datetime.datetime.now() > except Exception, e: > print 'error from Popen: ' + str(e) > sys.exit(1) > else: > print 'starting script.py for ' + name + ' at ' + > str(datetime.datetime.now()) > try: > self.procs[name] = subprocess.Popen(self.cmd) > self.startTimes[name] = datetime.datetime.now() > except Exception, e: > print 'error from Popen: ' + str(e) > sys.exit(1) > > time.sleep(self.sleepTime) > > > The script does what it's intended to do, however after about 2 hours > it has used up all the memory available and the machine hangs. Can > anyone see something that I am doing here that would be using memory > like this? Perhaps some immutable object needs to be repeatedly > recreated? I figured out what is causing this. Each pass through the loop it does: self.tools = Tool.objects.filter(ip__isnull=False) And that is what is causing the memory consumption. If I move that outside the loop and just do that once the memory issue goes away. Now I need to figure out why this is happening and how to prevent it as they do want to query the db each pass through the loop in case it has been updated.