Take a look at the code snippet below
Python 3.10.1 (main, Dec 10 2021, 10:36:36) [Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from shelve import DbfilenameShelf as db
>>> x = db("test") ## create a new Shelf object of type DbfilenameShelf
>>> x["k1"] = "v1" ## add some data to shelf object
>>> x
<shelve.DbfilenameShelf object at 0x105416260>
>>> dict(x) ## let's check what's in the shelf
{'k1': 'v1'}
>>> x.writeback ## writeback is set to False (the default value)
False
>>> x.close() ## cool .. let's close the shelf with writeback=False
>>> dict(x) ## this should not work anymore since shelf is closed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/_collections_abc.py", line 878, in __iter__
yield from self._mapping
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 95, in __iter__
for k in self.dict.keys():
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shelve.py", line 70, in closed
raise ValueError('invalid operation on closed shelf')
ValueError: invalid operation on closed shelf
>>> y = db("test") ## opening test.db again
>>> dict(y) ## how did shelf writeback to disk ?
{'k1': 'v1'}
>>> y.writeback ## writeback is still false
False
In the code snippet I create a new DbfilenameShelf with writeback=False
set in the Shelf object.
When I close the shelf object and reopen it, I see that data has persisted.
I thought for data to persist to disk writeback=True
is required. But that does not seem to be the case.
What's the point of writeback
if data in Shelf's cache gets flushed and written to disk anyways ?
You seem to have misinterpreted the meaning of writeback
.
Writeback
is a method of caching in which the latest data is initially written only to the cache and is written back to secondary storage only periodically or when some condition is satisfied. It is in contrast to write-through
method in which data is written to the secondary storage synchronously as soon as it is written to cache.
You can have a look at this answer.
You can also read the python docs for Shelve about the writeback parameter.
If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); this can make it handier to mutate mutable entries in the persistent dictionary, but, if many entries are accessed, it can consume vast amounts of memory for the cache, and it can make the close operation very slow since all accessed entries are written back