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


Groups > comp.lang.python > #86318

Re: Design thought for callbacks

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <fabiofz@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'nasty': 0.07; 'bits': 0.09; 'callback': 0.09; 'falls': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'creates': 0.14; '(note': 0.16; 'design?': 0.16; 'fabio': 0.16; 'thread,': 0.16; 'top-level': 0.16; 'url:py': 0.16; 'sat,': 0.16; 'weird': 0.16; 'url:)': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'all,': 0.19; '(where': 0.19; 'not,': 0.20; 'feb': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'earlier': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'certain': 0.27; 'developing': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'am,': 0.29; 'related': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'that.': 0.31; 'usually': 0.31; 'accidentally': 0.31; 'themselves': 0.32; 'url:python': 0.33; 'everyone': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'created': 0.35; 'objects': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'url:listinfo': 0.36; "didn't": 0.36; 'url:org': 0.36; 'should': 0.36; 'project': 0.37; 'url:library': 0.38; 'bad': 0.39; 'sure': 0.39; 'users': 0.40; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'worry': 0.60; 'url:3': 0.61; 'simple': 0.61; 'back': 0.62; 'kept': 0.65; 'to:addr:gmail.com': 0.65; 'subject:Design': 0.78; 'inform': 0.78; '2015': 0.84; '3.4': 0.84; 'about?': 0.84; 'preventing': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'url:master': 0.84; 'bounce': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=l9JtGvX/q5pbMh2W3GSaoi+3XrfhNK0TDocn2k9+RqI=; b=Gb0RUEiKP7cZkfUxvDP8/dU0VYE9C+8qCm0Udoi/93SWgtt5UQtCDxFWIu7+WsiN/K XQT1J0dDOu8Dyr37LYGMWgDUSja/8lBODWLiFCChUtSSicEwXSdq63a7VVCjkEvgs1ZG Ord072JpiRJzvtDKMWUyIXuaifAjFIbkaIYbQhv/201dIvUHy+NdpOXiZZnzWNH8D7Kd XHbLFVg6jxI7VmieCCE7rLmrlOrLsYqA4I+mojnNaw+jKla7nTje/UCH+i8+LRVOAFgZ yGqg2hh/mxUY4MqbqhBwxyQqM6uG6JNELDQGuwo+F6++7mNbxgtC5FYCrSpn8KN9nuBd NPEQ==
X-Received by 10.202.221.84 with SMTP id u81mr10564101oig.71.1424784213621; Tue, 24 Feb 2015 05:23:33 -0800 (PST)
MIME-Version 1.0
In-Reply-To <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com>
References <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com>
From Fabio Zadrozny <fabiofz@gmail.com>
Date Tue, 24 Feb 2015 10:23:13 -0300
Subject Re: Design thought for callbacks
To Cem Karan <cfkaran2@gmail.com>
Content-Type multipart/alternative; boundary=001a113ce330ef3ecb050fd56c38
Cc "comp.lang.python" <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.19128.1424785683.18130.python-list@python.org> (permalink)
Lines 108
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424785683 news.xs4all.nl 2947 [2001:888:2000:d::a6]:44044
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86318

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

Hi Cem,

I didn't read the whole long thread, but I thought I'd point you to what
I'm using in PyVmMonitor (http://www.pyvmmonitor.com/) -- which may already
cover your use-case.

Take a look at the callback.py at
https://github.com/fabioz/pyvmmonitor-core/blob/master/pyvmmonitor_core/callback.py

And its related test (where you can see how to use it):
https://github.com/fabioz/pyvmmonitor-core/blob/master/_pyvmmonitor_core_tests/test_callback.py
(note that it falls back to a strong reference on simple functions -- i.e.:
usually top-level methods or methods created inside a scope -- but
otherwise uses weak references).

Best Regards,

Fabio

On Sat, Feb 21, 2015 at 12:44 AM, Cem Karan <cfkaran2@gmail.com> wrote:

> 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?
>
> Thanks,
> Cem Karan
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: Design thought for callbacks Fabio Zadrozny <fabiofz@gmail.com> - 2015-02-24 10:23 -0300

csiph-web