Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'broken': 0.04; 'explicitly': 0.05; 'tree': 0.05; 'explicit': 0.07; 'level,': 0.07; 'generators': 0.09; 'handful': 0.09; 'references.': 0.09; 'subject:script': 0.09; 'trees': 0.09; 'uses.': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'gui': 0.12; 'creates': 0.14; 'callable': 0.16; 'callable,': 0.16; 'cleaned': 0.16; 'drop?': 0.16; 'event-driven': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'invisible': 0.16; 'itself,': 0.16; 'loop.': 0.16; 'subwindow.': 0.16; 'wider': 0.16; 'widget)': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'things.': 0.19; 'widget': 0.19; 'work,': 0.20; '>>>': 0.22; 'example': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'circular': 0.24; 'exists': 0.24; 'instance,': 0.24; 'logical': 0.24; 'please?': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'references': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'character': 0.29; 'generally': 0.29; 'room': 0.29; "doesn't": 0.30; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'comments': 0.31; 'that.': 0.31; 'usually': 0.31; '(my': 0.31; '(usually': 0.31; 'produces': 0.31; 'class': 0.32; 'subject:all': 0.32; 'up.': 0.33; 'fri,': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'basic': 0.35; "can't": 0.35; 'connection': 0.35; 'created': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doubt': 0.36; 'tight': 0.36; 'done': 0.36; 'useful': 0.36; 'similar': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'checks': 0.38; 'window': 0.38; 'issue': 0.38; 'track': 0.38; 'rather': 0.38; 'anything': 0.39; 'does': 0.39; 'extremely': 0.39; 'itself': 0.39; 'moving': 0.39; 'structure': 0.39; 'how': 0.40; 'ensure': 0.60; 'most': 0.60; 'break': 0.61; 'mentioned': 0.61; 'entire': 0.61; 'first': 0.61; 'hang': 0.67; 'mar': 0.68; 'fact,': 0.69; 'apart': 0.72; 'unusual': 0.74; 'cycles.': 0.84; "everything's": 0.84; 'pike': 0.84; 'to:none': 0.92; 'imagine': 0.93; 'room,': 0.93; 'connection,': 0.95; 'state.': 0.95 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:cc :content-type; bh=+Z8+EnI5g9O2pvHLaKXx0pTlqQM4OaafONwWrfMPk6c=; b=gBuVykqhrt+Ga5t4GRcWzjybb3AzyrwRZgzBDnP34vAz9+FNVETCtIurGB4AFPl8Hv DIPbx8pBFIYprSrOyWXXBYmdhw2be+ewgewYMixdhD5TJZMNaNEKdRmaT3wcMc+7lGmr rtLWuq+1OMxNT1P9konXB0kqaVz/SbQ3SFDCG/vI5jqNJtGWL0q4M97BQI55O17YPiv7 2YbZv8ZYygYqwiObuoztsvc9uHFF7sFF6b8VCmjLU8cc5MUV0bgaqoQzYDSC9I4hidBc 4q3+oFq+H9gqtcVvqd1A0WqcAMLNOaa5kMXHRXT4u8rggZb4G4to3VuvIlFuvdhh8IYy Es5A== MIME-Version: 1.0 X-Received: by 10.68.112.164 with SMTP id ir4mr17745304pbb.153.1394151076236; Thu, 06 Mar 2014 16:11:16 -0800 (PST) In-Reply-To: <878usmrijk.fsf@elektro.pacujo.net> References: <87ha7bq7ls.fsf@elektro.pacujo.net> <87d2hyrkf4.fsf@elektro.pacujo.net> <878usmrijk.fsf@elektro.pacujo.net> Date: Fri, 7 Mar 2014 11:11:16 +1100 Subject: Re: script uses up all memory From: Chris Angelico Cc: "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: 85 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394151084 news.xs4all.nl 2850 [2001:888:2000:d::a6]:47759 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67966 On Fri, Mar 7, 2014 at 10:53 AM, Marko Rauhamaa wrote: > Chris Angelico : > >> Can you give a useful example of a closure that does create a refloop? > > Just the other day, I mentioned the state pattern: > > class MyStateMachine: > def __init__(self): > sm = self > > class IDLE: > def ding(self): > sm.open_door() > sm.state = AT_DOOR() Yeah, that's an extremely unusual way to do things. Why keep on instantiating objects when you could just reference functions? > In general, event-driven programming produces circular references left > and right, and that might come into wider use with asyncio. Nope; certainly not with closures. I do a whole lot of event-driven programming (usually in Pike rather than Python, but they work the same way in this), and there's no reference loop. Properly-done event-driven programming should have two basic states: a reference from some invisible thing that can trigger the event (eg a GUI widget) to a callable, and a reference from that callable to its state. Once the trigger is gone, the callable is dropped, its state is dropped, and everything's cleaned up. You don't usually need a reference inside the function to that function. Don't forget, a closure need only hang onto the things it actually uses. It doesn't need all its locals. > I suspect generators might create circular references as well. I doubt it. >>> def foo(x): return ("x"*i for i in range(x)) >>> len([foo(5) for _ in range(1000)]) 1000 >>> gc.collect() 0 >>> len([foo(5) for _ in range(1000)]) 1000 >>> gc.collect() 0 Again, unless it keeps a reference to itself, there's no loop. It'll need to hang onto some of its locals, but that's all. > Any tree data structure with parent references creates cycles. Yes, but how many of those do you actually have and drop? If you create a GUI, you generally hold your entire widget tree stably. The only issue is if you create a parent-child subtree and then drop it. That shouldn't be being done in a tight loop. Most of the classic data structures like trees are implemented at the C level, so again, your code shouldn't be concerning itself with that. > In fact, I would imagine most OO designs create a pretty tight mesh of > back-and-forth references. Examples, please? I can think of a handful of situations where I've created reference loops, and they're sufficiently rare that I can put comments against them and explicitly break them. For instance, I have a "Subwindow" that has a "Connection". My window can have multiple subwindows, a subwindow may or may not have a connection, and the connection always references its subwindow. The subw->connection->subw loop is explicitly broken when the connection is terminated. If the window chooses to drop a subw, it first checks if there's a connection (and prompts the user to confirm), and then will explicitly disconnect, which breaks the refloop (as the connection's terminated). I did a similar thing at work, again with explicit refloop breakage to ensure clean removal. Apart from those two cases, I can't think of anything in the last ten years where I've had a data structure with a loop in it, where the whole loop could be dropped. (My MUD has a loop, in that a character exists in a room, and the room keeps track of its contents; but it's not logical to drop a room with characters in it, and dropping a character is done by moving it to no-room, which breaks the refloop.) ChrisA