Splitting up pickle files?

Erik Max Francis max at alcyone.com
Sun Feb 25 16:01:40 EST 2001


A very stateful server is running that I need to pickle and then
unpickle when the server is shutdown and restarted.  It is fairly
involved, as there are multiple "managers" (subdatabases) running that
need to keep track of their contents, and there is a lot of
cross-linking across these managers.

Pickling all the managers to one file and unpickling them works like a
charm, but I'd rather (for safety's sake, in case one gets corrupted)
pickle (effectively) each manager to a different file.  But since there
is a great deal of cross-linking, doing it naively would mean that
multiple (independent) instances of these cross-linked objects would end
up getting instantiated when the different files were unpickled. 
Obviously this is undesirable; when everything is unpickled, references
to objects in other managers should be just that, references, not
duplicates.

Is there an accepted way of doing this?  One approach (which should
work) I came up with is coming up with a Reference class that is created
with an (arbitrary) object, and implements various __getattr__-like
methods which defer to its contained object.  It would then implement
__getstate__ and __setstate__ so that when pickled, it would simply
return a string that represents the object it holds (i.e., just a string
ID that all objects would have), and which would use that string to look
up the object the ID represents in the appropriate manager.  It would be
a sort of enforced ownership; one manager owns the object in question,
and all other managers contain References to that object, so that at
pickling time things know where the object actually gets pickled, and
the others would just pickle explicit references to it.

It seems this approach would work, but it would be nice if there were an
easier (and less intrusive) way.  Any suggestions?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ It is human nature to think wisely and act foolishly.
\__/ Anatole France
    Computer science / http://www.alcyone.com/max/reference/compsci/
 A computer science reference.



More information about the Python-list mailing list