How can I avoid read errors when two python scripts access a single pickle file? -


i need pass data between 2 python scripts initiated 2 different sources. have seen logger read errors. there better way?

program a: regularly writes pickle file every minute.

def cachedata(filepath, data):     #create cpickle file cache current data     try:           outfile = open(filepath,'wb')           #cpickle current data file           cpickle.dump(data, outfile)           outfile.close()        except exception, e:           logger.warn("error creating cache file")   

program b: compiled executable initiated user. reads pickle file initiate code.

def readcachedobj(filepath):     #read cpickle file , return data object     try:         infile = open(filepath,'rb')         cache = cpickle.load(infile)         infile.close()         return cache     except exception, e:         logger.warn("error reading cached data cpickle") 

update 1

def replace(src, dst):     win32api.movefileex(src, dst, win32con.movefile_replace_existing)      def cachedata(filepath, data):     #create cpickle file cache current data     try:           tmpfile = str(uuid.uuid4()) + '.tmp'         outfile = open(tmpfile,'wb')           #cpickle current data temp file           cpickle.dump(data, outfile)           outfile.close()           #replace pickle file new temp file         replace(tmpfile, filepath)             #remove extraneous temp files         f in glob.glob("*.tmp"):             os.remove(f)             except exception, e:           logger.warn("error creating cache file")    def readcachedobj(filepath):     #read cpickle file , return data object     try:              infile = open(filepath,'rb')         cache = cpickle.load(infile)         infile.close()         return cache     except exception, e:         logger.warn("error reading cached data cpickle")       

never overwrite existing file. instead, write new file, , perform (atomic) rename after close it.


Comments