[Python-ideas] gc callbacks

Guido van Rossum guido at python.org
Mon Nov 29 06:43:03 CET 2010


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

AltStyle によって変換されたページ (->オリジナル) /