我需要在两个不同来源启动的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")
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")
答案 0 :(得分:1)
永远不会覆盖现有文件。而是写入 new 文件,然后在成功关闭它之后执行(原子)重命名。
答案 1 :(得分:0)
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")