当两个python脚本访问单个pickle文件时,如何避免读取错误?

时间:2015-07-10 23:48:19

标签: python pickle

我需要在两个不同来源启动的Python脚本之间传递数据。我看到我的记录器有读错误。还有更好的方法吗?

程序A:每分钟定期写入一个pickle文件。

def cacheData(filepath, data):
    #create a cPickle file to cache the current data
    try:  
        outFile = open(filepath,'wb')  
        #cPickle the current data to a file  
        cPickle.dump(data, outFile)  
        outFile.close()   
    except Exception, e:  
        logger.warn("Error creating cache file")  

程序B:是用户启动的已编译可执行文件。它读取pickle文件以启动一些代码。

def readCachedObj(filepath):
    #read cPickle file and return data as object
    try:
        inFile = open(filepath,'rb')
        cache = cPickle.load(inFile)
        inFile.close()
        return cache
    except Exception, e:
        logger.warn("Error reading cached data cPickle")

更新1

def replace(src, dst):
    win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)    

def cacheData(filepath, data):
    #create a cPickle file to cache the current data
    try:  
        tmpfile = str(uuid.uuid4()) + '.tmp'
        outFile = open(tmpfile,'wb')  
        #cPickle the current data to a temp file  
        cPickle.dump(data, outFile)  
        outFile.close()  
        #replace the pickle file with the new temp file
        replace(tmpfile, filepath)    
        #remove any extraneous temp files
        for f in glob.glob("*.tmp"):
            os.remove(f)        
    except Exception, e:  
        logger.warn("Error creating cache file")  

def readCachedObj(filepath):
    #read cPickle file and return data as object
    try:     
        inFile = open(filepath,'rb')
        cache = cPickle.load(inFile)
        inFile.close()
        return cache
    except Exception, e:
        logger.warn("Error reading cached data cPickle")      

2 个答案:

答案 0 :(得分:1)

永远不会覆盖现有文件。而是写入 new 文件,然后在成功关闭它之后执行(原子)重命名。

答案 1 :(得分:0)

这似乎对我有用。通过创建新的临时文件,然后使用文件覆盖,在处理最终的pickle文件时,cacheData函数会变得更加快捷。

def replace(src, dst):
    win32api.MoveFileEx(src, dst, win32con.MOVEFILE_REPLACE_EXISTING)    

def cacheData(filepath, data):
    #create a cPickle file to cache the current data
    try:  
        tmpfile = str(uuid.uuid4()) + '.tmp'
        outFile = open(tmpfile,'wb')  
        #cPickle the current data to a temp file  
        cPickle.dump(data, outFile)  
        outFile.close()  
        #replace the pickle file with the new temp file
        replace(tmpfile, filepath)    
        #remove any extraneous temp files
        for f in glob.glob("*.tmp"):
            os.remove(f)        
    except Exception, e:  
        logger.warn("Error creating cache file")  

def readCachedObj(filepath):
    #read cPickle file and return data as object
    try:     
        inFile = open(filepath,'rb')
        cache = cPickle.load(inFile)
        inFile.close()
        return cache
    except Exception, e:
        logger.warn("Error reading cached data cPickle")