dumping in destructor

Митя netimen at gmail.com
Tue Oct 21 14:31:58 CEST 2008


Thank you! I have already implemented custom load/save operations
without pickle. And they work fine on atexit.

Just for information: pickle.dump worked OK when called manually, but
being called by atexit it produeced the above described error. I don't
know why.
On Oct 21, 7:54 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 20 Oct 2008 10:01:07 -0200, Митя <neti... 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