Python酸洗词典

Python酸洗词典,python,dictionary,pickle,eoferror,Python,Dictionary,Pickle,Eoferror,我在服务器上运行了几个脚本,可以对各种字典进行pickle和unpickle。它们都使用相同的酸洗基本代码,如下所示: SellerDict=open('/home/hostadl/SellerDictkm','rb') SellerDictionarykm=pickle.load(SellerDict) SellerDict.close() SellerDict=open('/home/hostadl/SellerDictkm','wb') pickle.dump(SellerDiction

我在服务器上运行了几个脚本,可以对各种字典进行pickle和unpickle。它们都使用相同的酸洗基本代码,如下所示:

SellerDict=open('/home/hostadl/SellerDictkm','rb')
SellerDictionarykm=pickle.load(SellerDict)
SellerDict.close()

SellerDict=open('/home/hostadl/SellerDictkm','wb')
pickle.dump(SellerDictionarykm,SellerDict)
SellerDict.close()
除其中一个脚本外,所有脚本都运行良好。有问题的人会访问各种网站,收集数据并存储在字典中。这段代码运行一整天的酸洗和解酸洗字典,并在午夜停止。一个cronjob然后再次启动它 第二天早上。此脚本可以运行数周而不会出现问题,但大约每个月有一次,当脚本试图打开字典时,由于EOFError而导致脚本死机。字典的大小通常约为80MB。我甚至尝试在清理数据时在SellerDict.close()之前添加SellerDict.flush(),以确保数据被刷新

知道是什么导致了这一切吗?Python非常可靠,所以我认为这不是因为文件的大小。如果代码在死前很长一段时间都运行良好,这让我相信字典中可能保存了导致此问题的内容,但我不知道

另外,如果你知道一种比pickle更好的保存字典的方法,我愿意选择。正如我前面所说的,字典不断地被打开和关闭。为了澄清,只有一个程序会使用同一个字典,因此问题不是由多个程序试图访问同一个字典引起的

更新:

这是我从日志文件得到的回溯

Traceback (most recent call last):
  File "/home/hostadl/CompileRecentPosts.py", line 782, in <module>
    main()
  File "/home/hostadl/CompileRecentPosts.py", line 585, in main
    SellerDictionarykm=pickle.load(SellerDict)
EOFError
回溯(最近一次呼叫最后一次):
文件“/home/hostadl/CompileRecentPosts.py”,第782行,在
main()
文件“/home/hostadl/CompileRecentPosts.py”,第585行,主目录
SellerDictionarykm=pickle.load(SellerDict)
伊奥费罗

以下是不使用锁定时发生的情况:

import pickle

# define initial dict
orig_dict={'foo':'one'}

# write dict to file
writedict_file=open('./mydict','wb')
pickle.dump(orig_dict,writedict_file)
writedict_file.close()

# read the dict from file
readdict_file=open('./mydict','rb')
mydict=pickle.load(readdict_file)
readdict_file.close()

# now we have new data to save
new_dict={'foo':'one','bar':'two'}
writedict_file=open('./mydict','wb')
#pickle.dump(orig_dict,writedict_file)
#writedict_file.close()

# but...whoops!  before we could save the data
# some other reader tried opening the file
# now they are having a problem
readdict_file=open('./mydict','rb')
mydict=pickle.load(readdict_file) # errors out here
readdict_file.close()
以下是输出:

python pickletest.py
Traceback (most recent call last):
  File "pickletest.py", line 26, in <module>
    mydict=pickle.load(readdict_file) # errors out here
  File "/usr/lib/python2.6/pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.6/pickle.py", line 858, in load
    dispatch[key](self)
  File "/usr/lib/python2.6/pickle.py", line 880, in load_eof
    raise EOFError
EOFError
python pickletest.py
回溯(最近一次呼叫最后一次):
文件“pickletest.py”,第26行,在
mydict=pickle.load(readdict_文件)#此处出错
文件“/usr/lib/python2.6/pickle.py”,第1370行,已加载
返回Unpickler(file.load())
文件“/usr/lib/python2.6/pickle.py”,第858行,已加载
调度[键](自身)
文件“/usr/lib/python2.6/pickle.py”,第880行,装入
提高采收率
伊奥费罗
最后,某个读进程将尝试在某个写进程已经打开可写的情况下读取该pickle文件。在尝试读取之前,您需要确保有某种方法来判断另一个进程是否已打开一个文件进行写入


要获得一个非常简单的解决方案,请查看。

这实际上是一个内存问题。当计算机耗尽RAM并尝试解除锁定或加载数据时,该过程将无法获得此EOR。我增加了计算机上的RAM,这再也不是问题了


谢谢你的评论和帮助

你在使用任何锁定策略吗?我没有使用任何锁定策略…AJ,我感谢你的回答。但是,每个字典只有一个程序使用。因此,没有其他进程尝试读取或打开当前进程正在使用或写入的词典。您的问题中是否可以包含完整的回溯?我已使用回溯更新了原始问题。谢谢你的帮助!平均来说,你正在整理和拆开的字典有多大?