Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1a.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; '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; 'thu,': 0.19; '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; 'returned': 0.30; 'skip:( 40': 0.30; 'message- id:@mail.gmail.com': 0.30; 'apparently': 0.31; '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; 'there': 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; 'different.': 0.84; 'forks': 0.84; 'recover': 0.91 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=YXXgjzZenH5jhdQbOqlX9WnOb/Bp/FhxC8yknMOOkV8=; b=ivNU5VQxLWWdXYtehQCr5Cv8md1zRJZsP9WwTN6lLCkVdq97Pj287QObW+SoBbvyWr b3+cjqpa7adq+5BXVt8VHHFtWUzhPNioomMErHK9T5UKMjp02A7hDWMxUa2WFMxhQoEd l4h3kFukvo2XLcMnK0homyCXSwHh9oJejMQHjoi6ixTX07iHH+1yTGgVwdWafj6H3cVB LNUt5Th2PrdlcwKZeFyQ3yOkES/sJdzqCmTWyX0nJvVYKynePjuPPB5WQyO9dSeVAgFk x2m7H9vqgdBJTGbE8OwVsS1azMBFtZShVFUA4cLXVM/xN7c3o5tDXrMQCf+9GK7IVN2N PVqw== MIME-Version: 1.0 X-Received: by 10.194.87.163 with SMTP id az3mr13792386wjb.63.1394143621323; Thu, 06 Mar 2014 14:07:01 -0800 (PST) In-Reply-To: References: Date: Thu, 6 Mar 2014 17:07:01 -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: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394143628 news.xs4all.nl 2844 [2001:888:2000:d::a6]:44713 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67954 On Thu, Mar 6, 2014 at 4:56 PM, Larry Martell wrote: > 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. Apparently the object returned by that call is immutable as if I look at id(self.tools) each pass through the loop, it is different. Is there some way I can recover that memory?