简体   繁体   English

python 3.2 pickle.load随机导致EOFError

[英]python 3.2 pickle.load results in EOFError randomly

I encounter a very strange error. 我遇到一个非常奇怪的错误。 I have a cronjob that run daily: it opens a pickled file, load that file and then process the data. 我有一个每天运行的cronjob:它打开一个pickle文件,加载该文件,然后处理数据。 At the end of the script, it will save the data to that file for next day processing. 在脚本结束时,它会将数据保存到该文件以供第二天处理。

Most of the time, this script runs fine but there's some time (I would say twice a month) when opening that file, an EOFError is raised: 大多数时候,这个脚本运行正常但是有一些时间(我会说每月两次)打开该文件时,会引发一个EOFError:

Traceback (most recent call last):
  File "read.py", line 11, in <module>
    SellerDictionarycf=pickle.load(SellerDict)
EOFError

I strongly believe that there's no other process is working with that file and I'm 100% sure that I write the data, close that file before reading it. 我坚信没有其他进程正在使用该文件,我100%确定我写入数据,在阅读之前关闭该文件。

My code snipet to read the file at beginning of the script: 我的代码snipet在脚本开头读取文件:

SellerDict=open('/home/hostadl/SellerDictab','rb')
SellerDictionaryab=pickle.load(SellerDict)
SellerDict.close()

My code snipet to write the file at the end of the script: 我的代码snipet在脚本末尾写入文件:

SellerDict=open('/home/hostadl/SellerDictab','wb')
pickle.dump(SellerDictionaryab,SellerDict)
SellerDict.flush()
SellerDict.close()

I saved a copy of the corrupt file (file that raise EOFError when reading) and then check it with pickletools and here is the error: 我保存了一个损坏文件的副本(读取时引发EOFError的文件),然后用pickletools检查它,这是错误:

python3.2 -m pickletools -o test.txt SellerDictab
Traceback (most recent call last):
  File "/usr/local/lib/python3.2/runpy.py", line 160, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/local/lib/python3.2/runpy.py", line 73, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.2/pickletools.py", line 2403, in <module>
    args.indentlevel, annotate)
  File "/usr/local/lib/python3.2/pickletools.py", line 1963, in dis
    for opcode, arg, pos in genops(pickle):
  File "/usr/local/lib/python3.2/pickletools.py", line 1874, in genops
    arg = opcode.arg.reader(pickle)
  File "/usr/local/lib/python3.2/pickletools.py", line 474, in read_unicodestring4
    "remain" % (n, len(data)))
ValueError: expected 2254 bytes in a unicodestring4, but only 0 remain

The test.txt still has data and here is the head and tail: test.txt仍然有数据,这里是head和tail:

head test.txt
    0: \x80 PROTO      3
    2: }    EMPTY_DICT
    3: q    BINPUT     0
    5: (    MARK
    6: X        BINUNICODE 'bradey4357604769'
   27: q        BINPUT     1
   29: ]        EMPTY_LIST
   30: q        BINPUT     2
   32: ]        EMPTY_LIST
   33: q        BINPUT     3

tail test.txt
18933166: e                APPENDS    (MARK at 18930621)
18933167: ]            EMPTY_LIST
18933168: r            LONG_BINPUT 174342
18933173: (            MARK
18933174: X                BINUNICODE 'HP PhotoSmart C4480 All-in-One Printer'
18933217: r                LONG_BINPUT 174343
18933222: G                BINFLOAT   45.0
18933231: G                BINFLOAT   84104.0
18933240: X                BINUNICODE 'Salt Lake City,  UT '
18933265: r                LONG_BINPUT 174344

For the good file (with no EOFError) here is the head and tail: 对于好的文件(没有EOFError),这里是头部和尾部:

head testgood.txt
    0: \x80 PROTO      3
    2: }    EMPTY_DICT
    3: q    BINPUT     0
    5: (    MARK
    6: X        BINUNICODE 'bj8016541577'
   23: q        BINPUT     1
   25: ]        EMPTY_LIST
   26: q        BINPUT     2
   28: (        MARK
   29: ]            EMPTY_LIST

tail testgood.txt
16569368: X                BINUNICODE 'Bought this beautiful dinner set but never opened it, I would love to get rod of it pleas tex with an offer...'
16569483: r                LONG_BINPUT 161843
16569488: J                BININT     20177894
16569493: X                BINUNICODE 'antonio8016497082'
16569515: r                LONG_BINPUT 161844
16569520: e                APPENDS    (MARK at 16569283)
16569521: e            APPENDS    (MARK at 16568508)
16569522: u        SETITEMS   (MARK at 16088830)
16569523: .    STOP

So what could be the cause of this problem (like strange characters in the string data)? 那么问题的原因是什么(比如字符串数据中的奇怪字符)? I have no clue on this problem. 我对这个问题一无所知。

Looks like you ran out of disk space (or were unable to write the complete file for some similar reason). 看起来你的磁盘空间不足(或由于某些类似的原因无法编写完整的文件)。

A more idiomatic version (where you wouldn't need to worry about calling close/flush) would be: 一个更惯用的版本(你不需要担心调用close / flush)将是:

with open('/home/hostadl/SellerDictab','rb') as SellerDict:
    SellerDictionaryab=pickle.load(SellerDict)

with open('/home/hostadl/SellerDictab','wb') as SellerDict:
    pickle.dump(SellerDictionaryab,SellerDict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM