dumping in destructor

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Oct 20 23:54:52 EDT 2008


En Mon, 20 Oct 2008 10:01:07 -0200, Митя <netimen at gmail.com> escribió:

> Thank you for your answers!
>
> my g_register is a global object, and it lives all the program's
> lifetime, so 'with' is not appliable. Am I right?

Why not? You could use a with statement (or try/finally) around your main  
entry point.

> I tried to use atexit and wrote following:
>
> class _Register(object):
>    def dump(self):
>          ....
>
> class Registerable(object):
>    ....
>
> g_register = _Register()
> atexit.register(g_register.dump)
> ...
> ...
> g_register.add(Registerable('aa'))
>
> But now I get:
>
> cPickle.PicklingError: Can't pickle <class '__main__.Registerable'>:
> attribute lookup __main__.Registerable failed
>
> Does that mean that by the time of atexit execution my Registerable
> class is already dead?

No, at least not due to using atexit. When atexit functions are executed,  
the interpreter is still in a fully working state. From pythonrun.c,  
function Py_Finalize:

	/* The interpreter is still entirely intact at this point, and the
	 * exit funcs may be relying on that.  In particular, if some thread
	 * or exit func is still waiting to do an import, the import machinery
	 * expects Py_IsInitialized() to return true.  So don't say the
	 * interpreter is uninitialized until after the exit funcs have run.
	 * Note that Threading.py uses an exit func to do a join on all the
	 * threads created thru it, so this also protects pending imports in
	 * the threads created via Threading.
	 */

Probably you have another problem in your code; try to use pickle alone  
(not within atexit) and see what happens.

-- 
Gabriel Genellina




More information about the Python-list mailing list