Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86113

Re: Design thought for callbacks

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cfkaran2@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '"""': 0.07; '21,': 0.07; 'attribute': 0.07; '22,': 0.09; 'anymore.': 0.09; 'assumed': 0.09; 'references.': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'question.': 0.14; 'called.': 0.16; 'etc.),': 0.16; 'garbage': 0.16; 'lazily': 0.16; 'true),': 0.16; 'x()': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'library': 0.18; 'meant': 0.20; 'feb': 0.22; '>>>': 0.22; 'saying': 0.22; 'cc:addr:python.org': 0.22; 'sorry,': 0.24; 'regardless': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'query': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'supposed': 0.32; 'knows': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; '(e.g.,': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'so,': 0.37; 'message-id:@gmail.com': 0.38; 'problems': 0.38; 'handle': 0.38; 'anything': 0.39; 'system.': 0.39; 'how': 0.40; 'ensure': 0.60; 'eventually': 0.60; 'times': 0.62; "you've": 0.63; 'header:Message-Id:1': 0.63; 'manner': 0.72; 'subject:Design': 0.78; 'loose': 0.84; 'subject:thought': 0.84; 'careful': 0.91; 'state.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=6bkPrUK9kVMpzQc7Ipbp6GbEb26r5bX1wJbJ2j0a1U4=; b=eRzFar/hkMBNJY961hUty+y7JlgsMu65rDtUknGv5fWzIwZy6Dra8hHSJ4alcMSmP8 aV+oCqOlNs4NZu+CahSAvsfAOpGXdaanKGzmyVotw8fYaauTT1XGHHs0dX2aehHF9F0j iGf080sYdqug1H5XcIxaC2TI9I5FaMJ+OShxsnnrQtSJl02upqZ/rvco+k5FuGWybk28 4TzFuQS3Uh9/wlxirM0iNigmv/ttiahMk1wYVypDxX4nVRqa92Cd2+E4Aq7uB6IajEdx kYAORxsUB8pGaz4ITAv70QRfOdH4bk5jjtalt75NRx1S3594AT3E8DbdaYS7GJHt792L JdEA==
X-Received by 10.52.10.229 with SMTP id l5mr6296868vdb.52.1424612531708; Sun, 22 Feb 2015 05:42:11 -0800 (PST)
Content-Type text/plain; charset=us-ascii
Mime-Version 1.0 (Mac OS X Mail 6.6 \(1510\))
Subject Re: Design thought for callbacks
From Cem Karan <cfkaran2@gmail.com>
In-Reply-To <87k2za9mbf.fsf@elektro.pacujo.net>
Date Sun, 22 Feb 2015 08:42:10 -0500
Content-Transfer-Encoding quoted-printable
References <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <mailman.18948.1424497381.18130.python-list@python.org> <87zj87i74p.fsf@elektro.pacujo.net> <mailman.19002.1424607035.18130.python-list@python.org> <87k2za9mbf.fsf@elektro.pacujo.net>
To Marko Rauhamaa <marko@pacujo.net>
X-Mailer Apple Mail (2.1510)
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.19010.1424612539.18130.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424612539 news.xs4all.nl 2938 [2001:888:2000:d::a6]:60224
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86113

Show key headers only | View raw


On Feb 22, 2015, at 7:12 AM, Marko Rauhamaa <marko@pacujo.net> wrote:

> Cem Karan <cfkaran2@gmail.com>:
> 
>> On Feb 21, 2015, at 11:03 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>>> I use callbacks all the time but haven't had any problems with strong
>>> references.
>>> 
>>> I am careful to move my objects to a zombie state after they're done so
>>> they can absorb any potential loose callbacks that are lingering in the
>>> system.
>> 
>> So, if I were designing a library for you, you would be willing to have
>> a 'zombie' attribute on your callback, correct? This would allow the
>> library to query its callbacks to ensure that only 'live' callbacks are
>> called. How would you handle closures?
> 
> Sorry, don't understand the question.

You were saying that you move your objects into a zombie state.  I assumed that you meant you marked them in some manner (e.g., setting 'is_zombie' to True), so that anything that has a strong reference to the object knows the object is not supposed to be used anymore.  That way, regardless of where or how many times you've registered your object for callbacks, the library can do something like the following (banged out in my mail application, may have typos):

"""
_CALLBACKS = []

def execute_callbacks():
    global _CALLBACKS
    _CALLBACKS = [x for x in _CALLBACKS if not x.is_zombie]
    for x in _CALLBACKS:
        x()
"""

That will lazily unregister callbacks that are in the zombie state, which will eventually lead to their collection by the garbage collector.  It won't work for anything that you don't have a reference for (lambdas, etc.), but it should work in a lot of cases.

Is this what you meant?

Thanks,
Cem Karan

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Design thought for callbacks Chris Angelico <rosuav@gmail.com> - 2015-02-21 16:42 +1100
  Re: Design thought for callbacks Marko Rauhamaa <marko@pacujo.net> - 2015-02-21 18:03 +0200
    Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 07:10 -0500
      Re: Design thought for callbacks Marko Rauhamaa <marko@pacujo.net> - 2015-02-22 14:12 +0200
        Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 08:42 -0500
          Re: Design thought for callbacks Marko Rauhamaa <marko@pacujo.net> - 2015-02-22 15:54 +0200

csiph-web