当两个python脚本访问单个pickle文件时,如何避免读取错误?
我需要在从两个不同来源启动的两个Python脚本之间传递数据。我看到我的记录器出现读取错误。有更好的办法吗 程序A:大约每分钟都会定期写入pickle文件当两个python脚本访问单个pickle文件时,如何避免读取错误?,python,pickle,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
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
永远不要覆盖现有文件。相反,请写入一个新文件,然后在成功关闭它后执行(原子)重命名。这似乎适合我。通过创建新的临时文件,然后使用文件覆盖,cacheData函数在处理最终的pickle文件时非常快速。
大概但你不是这样问的。你现在真的有问题吗?是的,我在阅读方面看到了错误。我正在发布我的代码,并已将其重新格式化。第一次关于堆栈溢出-感谢您的耐心。我发现Python2.7在windows上需要Win32,并按照更新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")
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")