简体   繁体   English

读取腌制数据时出现EOFError

[英]EOFError when reading pickled data

I'm trying to unpickle chunks of a webpage stored in the Google App Engine memcache. 我正在尝试释放存储在Google App Engine内存缓存中的网页的内容。 First I get the chunks and store them as a dictionary with the key 首先,我得到块并将其存储为带有密钥的字典

def get_by_key_name(key_name):
    result = memcache.get_multi(['%s.%s' % (key_name, i) for i in xrange(32)])
    serialized = ''
    for k, v in sorted(result.items()):
        if v is not None:
            serialized = serialized.join(v)
        else:
            return None

    return pickle.loads(serialized) #Line that fails

For some reason it raises EOFError. 由于某种原因,它将引发EOFError。 The code that originally pickled the data is: 最初腌制数据的代码是:

serialized = pickle.dumps(content, 2)
values = {}
for i in xrange(0, len(serialized), chunksize):
  values['%s.%s' % (key_name, i//CHUNKSIZE) ] = serialized[i:i+chunksize]

Anybody have any idea why? 有人知道为什么吗? By the way, CHUNKSIZE is 950000 bytes. 顺便说一下,CHUNKSIZE是950000字节。 I tried to load reddit's front page onto the memcache, so I don't think it is exceeding this limit. 我试图将reddit的首页加载到内存缓存中,所以我认为它不会超出此限制。

You want to concatenate the string, not join. 您想连接字符串,而不是连接。

serialized += v

Join will add a copy of the original string between each character of the new string Join将在新字符串的每个字符之间添加原始字符串的副本

>>> 'hello'.join('there')
'thellohhelloehellorhelloe'

I'm kinda impressed you didn't run out of memory! 我对您没有用完内存留下深刻的印象!

You are joining your string incorrectly: 您输入的字符串不正确:

serialized = ''
for k, v in sorted(result.items()):
    if v is not None:
        serialized = serialized.join(v)

This uses selialized as built so far as the joining string, with the new string treated as individual characters: 这将使用selialized到连接字符串为止,新字符串被视为单个字符:

>>> serialized = ''
>>> for v in ('foo', 'bar', 'baz'):
...     serialized = serialized.join(v)
... 
>>> serialized
'bbfooafoorabfooafoorz'

where 'foo'.join('bar') produced 'bfooafoor' , which then was used to join the characters of baz . 其中'foo'.join('bar')产生了'bfooafoor' ,然后将其用于连接baz的字符。

Build a list , then return that: 建立一个清单 ,然后返回:

if None in result.viewvalues():
    # one or more keys came back empty, abort
    return
serialized = ''.join([v for k, v in sorted(result.items())])

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

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