Is it possible to detect corrupt Python dictionaries -
i have data file saved using shelve module in python 2.7 somehow corrupt. can load db = shelve.open('file.db')
when call len(db)
or bool(db)
hangs, , have kill process.
however, able loop through entire thing , create new non-corrupt file:
db = shelve.open('orig.db') db2 = shelve.open('copy.db') k, v in db.items(): db2[k] = v db2.close() # copy.db working copy
the question is, how can test dict , avoid hang?
btw, still have original file, , exhibits same behaviour when copied other machines, in case wants me bottom of what's wrong file in first place!
i'm unaware of inspection methods other dbm.whichdb(). debugging possible pickle protocol mismatch in manner allows timeout long running tests maybe try:
import shelve import pickle import dbm import multiprocessing import time import psutil def protocol_check(): print('orig.db is', dbm.whichdb('orig.db')) print('copy.db is', dbm.whichdb('copy.db')) p in range(pickle.highest_protocol + 1): print('trying protocol', p) db = shelve.open('orig.db', protocol=p) db2 = shelve.open('copy.db') try: k, v in db.items(): db2[k] = v finally: db2.close() db.close() print('great success on', p) def terminate(grace_period=2): procs = psutil.process().children() p in procs: p.terminate() gone, still_alive = psutil.wait_procs(procs, timeout=grace_period) p in still_alive: p.kill() process = multiprocessing.process(target=protocol_check) process.start() time.sleep(10) terminate()
Comments
Post a Comment