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


Groups > comp.lang.python > #86031

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!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'nasty': 0.07; '"if': 0.09; 'bits': 0.09; 'callback': 0.09; 'lawrence': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'wrote': 0.14; 'creates': 0.14; 'language.': 0.14; 'design?': 0.16; 'garbage': 0.16; 'gotcha.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'say.': 0.16; 'skip:n 50': 0.16; 'weird': 0.16; 'language': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; 'not,': 0.20; 'seems': 0.21; 'header:User- Agent:1': 0.23; 'earlier': 0.24; 'question': 0.24; 'first,': 0.26; 'certain': 0.27; 'developing': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'leave': 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; 'there': 0.35; 'version': 0.36; 'really': 0.36; "i'll": 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'project': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'bad': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'skip:u 10': 0.60; 'worry': 0.60; 'ago,': 0.61; 'url:3': 0.61; 'simple': 0.61; 'our': 0.64; 'kept': 0.65; 'charset:windows-1252': 0.65; 'frank': 0.68; 'qualified': 0.72; '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
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Design thought for callbacks
Date Sat, 21 Feb 2015 13:37:09 +0000
References <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <mc95pr$gau$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-92-24-222-48.ppp.as43234.net
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0
In-Reply-To <mc95pr$gau$1@ger.gmane.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.18960.1424525852.18130.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424525852 news.xs4all.nl 2901 [2001:888:2000:d::a6]:33468
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86031

Show key headers only | View raw


On 21/02/2015 05:41, Frank Millman wrote:
>
> "Cem Karan" <cfkaran2@gmail.com> wrote in message
> news:33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com...
>> Hi all, I'm working on a project that will involve the use of callbacks,
>> and I want to bounce an idea I had off of everyone to make sure I'm not
>> developing a bad idea.  Note that this is for python 3.4 code; I don't
>> need to worry about any version of python earlier than that.
>>
>> In order to inform users that certain bits of state have changed, I
>> require them to register a callback with my code.  The problem is that
>> when I store these callbacks, it naturally creates a strong reference to
>> the objects, which means that if they are deleted without unregistering
>> themselves first, my code will keep the callbacks alive.  Since this could
>> lead to really weird and nasty situations, I would like to store all the
>> callbacks in a WeakSet
>> (https://docs.python.org/3/library/weakref.html#weakref.WeakSet).  That
>> way, my code isn't the reason why the objects are kept alive, and if they
>> are no longer alive, they are automatically removed from the WeakSet,
>> preventing me from accidentally calling them when they are dead.  My
>> question is simple; is this a good design?  If not, why not?
>>    Are there any potential 'gotchas' I should be worried about?
>>
>
> I tried something similar a while ago, and I did find a gotcha.
>
> The problem lies in this phrase - "if they are no longer alive, they are
> automatically removed from the WeakSet, preventing me from accidentally
> calling them when they are dead."
>
> I found that the reference was not removed immediately, but was waiting to
> be garbage collected. During that window, I could call the callback, which
> resulted in an error.
>
> There may have been a simple workaround. Perhaps someone else can comment.
>
> Frank Millman
>

https://docs.python.org/3/library/gc.html has a collect function.  That 
seems like a simple workaround, but whether or not it classifies as a 
good solution I'll leave to others, I'm not qualified to say.


-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Thread

Re: Design thought for callbacks Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-21 13:37 +0000

csiph-web