[Twisted-Python] cleanup in twisted

Joachim Boomberschloss boomberschloss at yahoo.com
Wed May 25 09:12:16 EDT 2005


--- Jp Calderone <exarkun at divmod.com> wrote:

> On Wed, 25 May 2005 05:21:20 -0700 (PDT), Joachim
> Boomberschloss <boomberschloss at yahoo.com> wrote:
> >
> > ...
> >>
> >> I'm not sure if it's worth the effort.  I don't
> >> exactly see the attraction of the functionality
> >> being provided.  I tend to find that explicit
> >> cleanup is not overly burdensome.  Perhaps you
> can
> >> share some examples of how you see it being used?
> >
> >Well, the attraction is this: I ran into situations
> in
> >which it is desirable for an object to do some
> >communication-related cleanup when deleted, either
> >when the program shuts down or when it is no longer
> >referenced. For example, I have objects responsible
> >for maintaining communication with some servers,
> and I
> >want them to inform the server when they cease to
> >maintain communication with it. This should be done
> >either when the service is no longer necessary and
> the
> >object (the maintainer) is deleted, or when the
> >program shuts down, so basically, the functionality
> >required is to be able to define
> communication-related
> >stuff (i.e. things that aren't instantaneous and
> >involve deferreds) to be done either when the
> object
> >is deleted or when twisted shuts down. The latter
> is
> >easy to do, but the former isn't, and come to think
> of
> >it, my solution isn't good regardless of reference
> >cycles, because, since the cleanup method is
> spawned
> >from __del__, it has to do the cleanup
> instantaneously
> >or the cleanup process won't be able to refer to
> the
> >object at a later stage. Basically what is needed
> is
> >to be able to defer the destruction of the object
> both
> >at program shutdown, which is easy, and before
> object
> >deletion, which I don't know how to do. Any
> thoughts
> >(either about the attractiveness of the
> functionality
> >or about how to achieve it)?
> >
> 
> Rescuing an object from garbage collection is easier
> than you may expect:
> 
>   exarkun at boson:~$ python
>   Python 2.4.1 (#2, Mar 30 2005, 21:51:10) 
>   [GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
>   Type "help", "copyright", "credits" or "license"
> for more information.
>   >>> L = []
>   >>> class Foo:
>   ...     def __del__(self):
>   ...             L.append(self)
>   ... 
>   >>> f = Foo()
>   >>> del f
>   >>> L
>   [<__main__.Foo instance at 0xb7dff34c>]
>   >>> 
> 
> Of course, for it to ever be collected, you'll need
> to take it out of that list.
> 

Hmmm. Good to know. I thought this sort of thing was
considered illegal in Python. This resolves deferring
the destruction, but not the problem with reference
cycles. Is there any way to do it without __del__?

I thought of one crazy option: create a reference
cycle intentionally in the class' __init__, and then
run a looping call that will check gc.garbage from
time to time. If the object appears there, then we
know that it's safe to delete it; we can call the
__cleanup__ method and afterwards break the reference
cycle and delete the object.

Joe.


		
__________________________________
Discover Yahoo! 
Find restaurants, movies, travel and more fun for the weekend. Check it out! 
http://discover.yahoo.com/weekend.html 





More information about the Twisted-Python mailing list