私は、さまざまな辞書をpickleしたりunpickleするサーバ上でいくつかのスクリプトを実行しています。それらはすべて以下のように酸洗いに同じ基本コードを使用します:Python pickling dictionary EOFError
SellerDict=open('/home/hostadl/SellerDictkm','rb')
SellerDictionarykm=pickle.load(SellerDict)
SellerDict.close()
SellerDict=open('/home/hostadl/SellerDictkm','wb')
pickle.dump(SellerDictionarykm,SellerDict)
SellerDict.close()
すべてのスクリプトは、1つを除いて正常に実行されます。問題のあるものは、さまざまなウェブサイトに行き、データをスクラップして辞書に格納します。このコードでは、一日中の長時間の漬け物と解凍用の辞書を実行し、深夜に停止します。 cronjobはそれから再びそれを始める 翌朝。このスクリプトは問題なく何週間も実行できますが、辞書を開くときにEOFErrorが発生するため、月に約1回スクリプトが停止します。ディクショナリのサイズは通常約80 MBです。私はさらに、夕方が洗い流されていることを確認するためにデータをpicklingするときに、SellerDict.close()の前にSellerDict.flush()を追加しようとしました。
これを引き起こす原因は何でしょうか?私はそれがファイルのサイズに起因するとは思わないので、Pythonは非常にしっかりしています。死ぬ前にコードが長い間うまく動作する場所では、この問題の原因となっている辞書に何かが保存されている可能性があると私は信じていますが、わかりません。
また、pickle以外の辞書を保存するより良い方法を知っていれば、私はオプションを公開しています。私が以前に言ったように、辞書は絶えず開いて閉じています。明確にするために、1つのプログラムだけが同じ辞書を使用するので、問題は同じ辞書にアクセスしようとするいくつかのプログラムによって引き起こされていません。
UPDATE:ここ
私は、ログファイルから持っているトレースバックです。ここで
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()
出力です:
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
どのような種類のロック戦略を使用していますか? –
私はロック戦略を使用していません... – jordanskis