Skip to main content
 首页 » 编程设计

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

2025年05月04日8qq78292959

我需要在从两个不同来源启动的两个 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")       

请您参考如下方法:

切勿覆盖现有文件。相反,写入一个新文件,然后在成功关闭它后执行(原子)重命名。