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; 'explicitly': 0.05; '"""': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'apis': 0.09; 'callback': 0.09; 'caller': 0.09; 'latter': 0.09; 'msg': 0.09; 'pep': 0.09; 'read-only': 0.09; 'references.': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'stored': 0.12; 'both)': 0.16; 'callables': 0.16; 'called.': 0.16; 'determining': 0.16; 'fabio': 0.16; 'garbage': 0.16; 'inserting': 0.16; 'matters,': 0.16; 'msg):': 0.16; 'pythonic': 0.16; 'registered.': 0.16; 'retained': 0.16; 'retains': 0.16; 'sorts': 0.16; 'subclassing': 0.16; 'such,': 0.16; 'surprising': 0.16; 'unexpected': 0.16; 'url:peps': 0.16; 'url:pydev': 0.16; 'url:)': 0.16; 'thanks,': 0.17; 'library': 0.18; 'all,': 0.19; 'implementing': 0.19; 'pointed': 0.19; 'addition,': 0.20; 'appears': 0.22; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'adds': 0.24; 'finally,': 0.24; 'library,': 0.24; 'replace': 0.24; 'skip:l 30': 0.24; 'skip:{ 20': 0.24; 'url:02': 0.24; 'url:dev': 0.24; 'fairly': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'references': 0.26; 'least': 0.26; 'header:In- Reply-To:1': 0.27; 'record': 0.27; 'idea': 0.28; 'point': 0.28; 'properties': 0.29; "doesn't": 0.30; 'forgot': 0.30; 'originally': 0.30; 'said,': 0.30; 'strongly': 0.30; "i'm": 0.30; 'asked': 0.31; 'code': 0.31; 'getting': 0.31; 'comments': 0.31; 'occurs': 0.31; 'skip:= 20': 0.31; 'them?': 0.31; 'class': 0.32; 'maintaining': 0.32; 'skip:c 30': 0.32; 'supposed': 0.32; 'option': 0.32; 'another': 0.32; 'quite': 0.32; 'up.': 0.33; 'week.': 0.33; 'url:python': 0.33; 'everyone': 0.33; 'running': 0.33; 'becomes': 0.33; 'cases': 0.33; 'sense': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'knows': 0.35; 'problem.': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'library.': 0.36; 'maintained': 0.36; 'sequence': 0.36; 'set.': 0.36; 'method': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'responsible': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'half': 0.37; 'requirements': 0.37; 'list': 0.37; 'kind': 0.63; 'personal': 0.63; 'such': 0.63; 'soon': 0.63; 'decided': 0.64; 'more': 0.64; 'different': 0.65; 'taking': 0.65; 'url:blogspot': 0.65; 'finally': 0.65; 'due': 0.66; 'difficulty': 0.68; 'feeling': 0.68; 'realized': 0.68; 'results': 0.69; 'url:br': 0.69; 'carefully': 0.74; 'obvious': 0.74; 'increase': 0.74; 'hoping': 0.75; 'analysis': 0.75; 'subject:Design': 0.78; 'different.': 0.84; 'subject:thought': 0.84; 'weather': 0.84; 'careful': 0.91; 'involved.': 0.91; 'to:none': 0.92; 'reply,': 0.93 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; bh=pcb5hGXoaqIyFeB+2Mr1NFjc+sUZIimAbvZ6kg2RTmQ=; b=RXk4uHaQ24xzZu5sAc5l6rgDQdEykTJJN4w2xt6IpBiCNUv3MKRatfe9/XP3xCkkMG EdkrxJhy7eVoLsglGAA7Cc2TzyH9GycTyhdXugLE9JWcTk8/4EPdwx113N6L+/2BfQZ6 E3uXp312hwac0KUkCi/1XbvBMdEsog6uqIQJ3z0b2TeDHHVD5/brheop7EFsXEarvZvh nkQBERvzuMpVNvQxcePJUuIudydugTTwdDeHhuXGslm/Kcx5b+2AJrvOKSeiEQ0U7I78 I313PGjCZuQn6Zws9v9DJEPnDZ9EEdl+fPaMjv2N4UJzPWr3CZLIQgih0WFEWt/qMCmS hwAw== X-Received: by 10.229.96.194 with SMTP id i2mr27481339qcn.1.1425839435422; Sun, 08 Mar 2015 11:30:35 -0700 (PDT) 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 In-Reply-To: Date: Sun, 8 Mar 2015 14:30:32 -0400 Cc: Python Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <8A3659BC-9100-4A3A-9117-47227B3D290B@gmail.com> X-Mailer: Apple Mail (2.1510) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 129 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425839439 news.xs4all.nl 2939 [2001:888:2000:d::a6]:53781 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:87151 Hi all, I apologize for taking so long to reply, but neither my work = schedule nor the weather have been kind in the past week. That said, = I've been thinking long and hard about what everyone has said, and have = decided that it would be useful to write a wrap-up email that attempts = to encapsulate everything everyone has said, as a record of sorts if = nothing else. As such, this email is fairly long and involved. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Analysis of the problem =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D My original question was 'what is the least surprising/most pythonic way = to write a callback API?' Through reading what everyone has said, I = realized that I wasn't being specific enough, simply because callback = APIs can be quite different. At the very least, the following questions = need to be answered: 1) When a callback is registered, does it replace the prior callback? 2) If more than one callback can be registered, is there an ordering to = them? 3) Can a callback be registered more than once? 4) When and how are callbacks deregistered? 5) Who is responsible for maintaining a strong reference to the = callback? As far as I know, there isn't a standard method to indicate to the = caller that one callback replaces another one except via well-written = documentation. My personal feeling is that callbacks that replace other = callbacks should be properties of the library. By implementing a = setter, getter, and deleter for each callback, the library makes it = obvious that there is one and only one callback active at a time. The = only difficulty is making sure the user knows that the library retains = the callback, but this is a documentation problem. =20 I realized that ordering could be a problem when I read through the = documentation to asyncio.call_soon(). It promises that callbacks will = be called in the order in which they were registered. However, there = are cases where the order doesn't matter. Registration in both of these = cases is fairly simple; the former appends the callback to a list, while = the latter adds it to a set. The list or set can be a property of the = library, and registration is simply a matter of either inserting or = adding. But this brings up point 3; if a callback can be registered at = most once and ordering matters, then we need something that is both a = sequence and a set. Subclassing either (or both) = collections.abc.MutableSequence or collections.abc.MutableSet will lead = to confusion due to unexpected violations of PEP 3119 = (https://www.python.org/dev/peps/pep-3119/). Once again, the only = option appears to be careful documentation. Registration is only half the problem. The other half is determining = when a callback should be unregistered. Some callbacks are one-shots = and are automatically unregistered as soon as they are called. Others = will be called each time an event occurs until they are explicitly = unregistered from the library. Which happens is another design choice = that needs to be carefully documented. Finally, we come to the part that started my original question; who = retains the callback. I had originally asked everyone if it would be = surprising to store callbacks as weak references. The idea was that = unless someone else maintained a strong reference to the callback, it = would be garbage collected, which would save users from 'surprising' = results such as the following: """ #! /usr/bin/env python class Callback_object(object): def __init__(self, msg): self._msg =3D msg def callback(self, stuff): print("=46rom {0!s}: {1!s}".format(self._msg, stuff)) class Fake_library(object): def __init__(self): self._callbacks =3D list() def register_callback(self, callback): self._callbacks.append(callback) def execute_callbacks(self): for thing in self._callbacks: thing('Surprise!') if __name__ =3D=3D "__main__": cbo =3D Callback_object("Evil Zombie") lib =3D Fake_library() lib.register_callback(cbo.callback) # Way later, after the user forgot all about the callback above cbo =3D Callback_object("Your Significant Other") lib.register_callback(cbo.callback) # And finally getting around to running all those callbacks. lib.execute_callbacks() """ However, as others pointed out using a weak reference could actually = increase confusion rather than decrease it. The problem is that if = there is a reference cycle elsewhere in the code, it is possible that = the zombie object is still alive when it is supposed to be dead. This = will likely be difficult to debug. In addition, different types of = callables have different requirements in order to correctly store weak = references to them. Both Ian Kelly and Fabio Zadrozny provided = solutions to this, with Fabio providing a link to his code at = http://pydev.blogspot.com.br/2015/02/design-for-client-side-applications-i= n.html. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Solution to my problem in particular =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D After considering all the comments above, I've decided to do the = following for my API: - All callbacks will be strongly retained (no weakrefs). - Callbacks will be stored in a list, and the list will be exposed as a = read-only property of the library. This will let users reorder = callbacks as necessary, add them multiple times in a row, etc. I'm also = hoping that by making it a list, it becomes obvious that the callback is = strongly retained. - Finally, callbacks are not one-shots. This just happens to make sense = for my code, but others may find other methods make more sense. Thanks again to everyone for providing so many comments on my question, = and I apologize again for taking so long to wrap things up. Thanks, Cem Karan=