Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86015
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Subject | Re: Design thought for callbacks |
| Date | 2015-02-21 07:41 +0200 |
| References | <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18947.1424497301.18130.python-list@python.org> (permalink) |
"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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Design thought for callbacks "Frank Millman" <frank@chagford.com> - 2015-02-21 07:41 +0200
Re: Design thought for callbacks Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-22 03:15 +1100
Re: Design thought for callbacks Marko Rauhamaa <marko@pacujo.net> - 2015-02-21 19:08 +0200
Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 07:16 -0500
Re: Design thought for callbacks Marko Rauhamaa <marko@pacujo.net> - 2015-02-22 14:46 +0200
Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 09:17 -0500
Re: Design thought for callbacks Laura Creighton <lac@openend.se> - 2015-02-22 13:52 +0100
Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 09:10 -0500
Re: Design thought for callbacks "Frank Millman" <frank@chagford.com> - 2015-02-22 08:44 +0200
Re: Design thought for callbacks Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-22 23:15 +1300
Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-22 09:22 -0500
Re: Design thought for callbacks Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-24 18:45 +1300
Re: Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-24 06:06 -0500
Re: Design thought for callbacks Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-22 23:57 -0700
csiph-web