I'm writing a program that caches some results via the pickle module. What happens at the moment is that if I hit ctrl-c at while the dump
operation is occurring, dump
gets interrupted and the resulting file is corrupted (i.e. only partially written, so it cannot be load
ed again.
Is there a way to make dump
, or in general a block of code, uninterruptable? My current workaround looks something like this:
try:
file = open(path, 'w')
dump(obj, file)
file.close()
except KeyboardInterrupt:
file.close()
file.open(path,'w')
dump(obj, file)
file.close()
raise
It seems silly to restart the operation if it is interrupted, so I am searching for a way to defer the interrupt. How do I do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…