Without shelf.sync():
def save(cls):
Cont.shelf['queue'] = cls.jobQueue #1
print(Cont.shelf['queue']) #2
# [1, 2, 3]
cls.jobQueue.pop() #3
print(Cont.shelf['queue']) #4
# [1, 2]
- With writeback=True, assignments store the key/value pair in shelf.cache as well as shelf.dict.
- Attempts to retrieve the data at key
'queue'
from shelf.cache. - modifies
cls.jobQueue
, which is the same object as the one retrieved from the cache - retrieves the data at key
'queue'
from shelf.cache again. Since the cache holds a refence tocls.jobQueue
, this is the same object.
However, if you call shelf.sync():
def save(cls):
Cont.shelf['queue'] = cls.jobQueue
Cont.shelf.sync() #1
print(Cont.shelf['queue']) #2
# [1, 2, 3]
cls.jobQueue.pop() #3
print(Cont.shelf['queue']) #4
# [1, 2, 3]
- The shelve file is updated, and the cache is reset to an empty dict.
- Attempts to retrieve the data at key
'queue'
from shelf.cache. Since the cache is empty, a new copy of the data is retrieved from the shelve file. - modifies
cls.jobQueue
, which is not the same object as the copy just retrieved - The cache is still empty, so this retrieves a new copy from the unupdated shelve file again