[Python-ideas] gc callbacks

Kristján Valur Jónsson kristjan at ccpgames.com
Mon Nov 29 07:01:39 CET 2010


I'll submit our current code as a patch to the tracker.  Like all our contributions it comes with no strings attached :)
K

-----Original Message-----
From: gvanrossum at gmail.com [mailto:gvanrossum at gmail.com] On Behalf Of Guido van Rossum
Sent: Monday, November 29, 2010 13:43
To: Kristján Valur Jónsson
Cc: python-ideas at python.org
Subject: Re: [Python-ideas] gc callbacks

Sounds great. One could quibble over the details of the callback signature, but I'll leave the bikeshedding to others. Do you have code that could be a basis for a patch? Is it encumbered by copyrights?

--Guido

2010/11/28 Kristján Valur Jónsson <kristjan at ccpgames.com>:
> Hi there.
>
> Some months back I mentioned that I had other stuff in store for GC.
>
> Here is an idea for you.  This particular idea is just a 
> generalization of a system we've used in EVE for years now: garbage collection callbacks.
>
>
>
> The need, originally, was to be able to quantify the time spent in 
> garbage collection runs.  Since they occur out of direct control of 
> the application, we needed to have python tell us about it somehow.
>
>
>
> We added gc.register_callback(), a function that added a callable to 
> an internal list of functions that get called on two occations:
>
> 1)      When a garbage collection run Is about to start
>
> 2)      When a garbage collection run has finished.
>
> The cases are distinguished using an integer argument.  The callbacks 
> are invoked from gc with gc inhibited from reentry, so that the 
> callbacks cannot themselves cause another gc run to commence.
>
>
>
> What we traditionally use this for is to start and stop a performance 
> timer and other stats.
>
> More recently though, we have found another very important use for this.
> When gc finds uncollectable objects, they are put in the gc.garbage list.
> This then needs to be handled by the application.  However, there is 
> no particularly good way for the application to do this, except to 
> periodically check this list.
>
>
>
> With the above callback, modules that create uncollectable objects, 
> such as classes with __del__ methods, can register their callback.  At 
> the end of a gc run, they can then walk gc.garbage and take 
> appropriate action for those objects it recognizes.
>
>
>
> So, what do you think?  This is  a very simple addition to gc, 
> orthogonal to everything and easily implemented.  I also think it is very useful.
>
>
>
> K
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>



--
--Guido van Rossum (python.org/~guido)




More information about the Python-ideas mailing list