[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

keys must further be str strings in Python 3.X, not bytes, because the shelve will at-
tempt to encode them in all cases.


Objects are unique only within a key


Although the shelve module is smart enough to detect multiple occurrences of a nested
object and re-create only one copy when fetched, this holds true only within a given slot:


dbase[key] = [object, object] # OK: only one copy stored and fetched

dbase[key1] = object
dbase[key2] = object # bad?: two copies of object in the shelve

When key1 and key2 are fetched, they reference independent copies of the original
shared object; if that object is mutable, changes from one won’t be reflected in the
other. This really stems from the fact the each key assignment runs an independent
pickle operation—the pickler detects repeated objects but only within each pickle call.
This may or may not be a concern in your practice, and it can be avoided with extra
support logic, but an object can be duplicated if it spans keys.


Updates must treat shelves as fetch-modify-store mappings


Because objects fetched from a shelve don’t know that they came from a shelve, oper-
ations that change components of a fetched object change only the in-memory copy,
not the data on a shelve:


dbase[key].attr = value # shelve unchanged

To really change an object stored on a shelve, fetch it into memory, change its parts,
and then write it back to the shelve as a whole by key assignment:


object = dbase[key] # fetch it
object.attr = value # modify it
dbase[key] = object # store back-shelve changed (unless writeback)

As noted earlier, the shelve.open call’s optional writeback argument can be used to
avoid the last step here, by automatically caching objects fetched and writing them to
disk when the shelve is closed, but this can require substantial memory resources and
make close operations slow.


Concurrent updates are not directly supported


The shelve module does not currently support simultaneous updates. Simultaneous
readers are OK, but writers must be given exclusive access to the shelve. You can trash
a shelve if multiple processes write to it at the same time, which is a common potential
in things such as server-side scripts on the Web. If your shelves may be updated by
multiple processes, be sure to wrap updates in calls to the os.open standard library
function to lock files and provide exclusive access.


1322 | Chapter 17: Databases and Persistence

Free download pdf