Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '21,': 0.07; 'debugging': 0.07; 'nasty': 0.07; '"if': 0.09; 'bits': 0.09; 'callback': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'wrote': 0.14; 'creates': 0.14; 'design?': 0.16; 'garbage': 0.16; 'gotcha.': 0.16; 'skip:n 50': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'not,': 0.20; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'earlier': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'first,': 0.26; 'certain': 0.27; 'developing': 0.27; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'am,': 0.29; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'accidentally': 0.31; 'lies': 0.31; 'themselves': 0.32; 'url:python': 0.33; 'everyone': 0.33; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'charset:us- ascii': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'project': 0.37; 'message-id:@gmail.com': 0.38; 'url:library': 0.38; 'bad': 0.39; 'sure': 0.39; 'users': 0.40; 'skip:u 10': 0.60; 'worry': 0.60; 'ago,': 0.61; 'url:3': 0.61; 'simple': 0.61; 'header:Message-Id:1': 0.63; 'kept': 0.65; 'frank': 0.68; 'subject:Design': 0.78; 'inform': 0.78; '3.4': 0.84; 'about?': 0.84; 'comment.': 0.84; 'preventing': 0.84; 'resulted': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'bounce': 0.91 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=Uolq0zxxGgaDyRzT05oGTiyqEF/ftdkkTB2Y5+YI4/E=; b=cQ/X/+TEnxDgiWC2OLo8G7nsk0CC+tTr4eJpt4q6SM+rW8ZKrch7FlDZ3OrT+GpA2+ 5lfD+0LKsXP9lENXybXOSc84qnhcsmJGEWI9VFoKT4s8vdFCQYbzLgLgTVIsARuzZ8Aw aZIYnnZQqbnjqXZYhIjkysjwFeOnDTeb2aNPaWR+Xt9awcsAFV9RNdX2LVI/nb7do3Ge y2Z06vTzhqF7b5wdnIMlwDXCmWFrw+7kFuy6Mcm8bC6IoCAh3TaGyVMrKZiWotHNbeM7 ovxypgDJZtFFHm3nn6PHLkmfpB4nSv3jm9G3Q6QX65L3B/ZSxuq+eduB2/KAbhe6XKX7 tdMw== X-Received: by 10.140.42.130 with SMTP id c2mr5091056qga.105.1424524444578; Sat, 21 Feb 2015 05:14:04 -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 In-Reply-To: Date: Sat, 21 Feb 2015 08:14:03 -0500 Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> To: Frank Millman 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424524867 news.xs4all.nl 2924 [2001:888:2000:d::a6]:57884 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86030 On Feb 21, 2015, at 12:41 AM, Frank Millman wrote: >=20 > "Cem Karan" wrote in message=20 > news:33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com... >> Hi all, I'm working on a project that will involve the use of = callbacks,=20 >> and I want to bounce an idea I had off of everyone to make sure I'm = not=20 >> developing a bad idea. Note that this is for python 3.4 code; I = don't=20 >> need to worry about any version of python earlier than that. >>=20 >> In order to inform users that certain bits of state have changed, I=20= >> require them to register a callback with my code. The problem is = that=20 >> when I store these callbacks, it naturally creates a strong reference = to=20 >> the objects, which means that if they are deleted without = unregistering=20 >> themselves first, my code will keep the callbacks alive. Since this = could=20 >> lead to really weird and nasty situations, I would like to store all = the=20 >> callbacks in a WeakSet=20 >> (https://docs.python.org/3/library/weakref.html#weakref.WeakSet). = That=20 >> way, my code isn't the reason why the objects are kept alive, and if = they=20 >> are no longer alive, they are automatically removed from the WeakSet,=20= >> preventing me from accidentally calling them when they are dead. My=20= >> question is simple; is this a good design? If not, why not? >> Are there any potential 'gotchas' I should be worried about? >>=20 >=20 > I tried something similar a while ago, and I did find a gotcha. >=20 > The problem lies in this phrase - "if they are no longer alive, they = are=20 > automatically removed from the WeakSet, preventing me from = accidentally=20 > calling them when they are dead." >=20 > I found that the reference was not removed immediately, but was = waiting to=20 > be garbage collected. During that window, I could call the callback, = which=20 > resulted in an error. >=20 > There may have been a simple workaround. Perhaps someone else can = comment. THAT would be one heck of a gotcha! Must have been fun debugging that = one! Cem Karan=