【问题标题】:App Engine - getting KeyError with memcache implementationApp Engine - 使用 memcache 实现获取 KeyError
【发布时间】:2012-07-14 16:03:49
【问题描述】:

我正在尝试实现 Guido 作为问题 Avoiding Memcache 1M limit of values 的答案发布的代码。当我第一次加载页面并向内存缓存添加值时,一切似乎(?)都在工作,但是当我重新加载它并从内存缓存中检索值时,我收到了一个奇怪的错误:

Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 194, in Handle
    for chunk in result:
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/appstats/recording.py", line 1036, in appstats_wsgi_wrapper
    result = app(environ, appstats_start_response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/appengineurlhere/mypythoncode.py", line 87, in get
    entitylist = retrieve("entitylist")
  File "/appengineurlhere/mypythoncode.py", line 53, in retrieve
    return pickle.loads(serialized)
  File "/base/python27_runtime/python27_dist/lib/python2.7/pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "/base/python27_runtime/python27_dist/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
KeyError: ':'

这是存储/检索代码:

def store(key, value, chunksize=750000):
    serialized = pickle.dumps(value, 2)
    values = {}
    for i in xrange(0, len(serialized), chunksize):
        values['%s.%s' % (key, i//chunksize)] = serialized[i : i+chunksize]
    memcache.set_multi(values)

def retrieve(key):
    result = memcache.get_multi(['%s.%s' % (key, i) for i in xrange(32)])
    serialized = ''.join([v for v in result.values() if v is not None])
    return pickle.loads(serialized)

这就是我使用它的方式:

try:
  entitylist = retrieve("entitylist")
except EOFError:
  entitylist = MyModel.all().fetch(None)
  store("entitylist", entitylist)

其他怪事:

  • 我在我的开发服务器上看不到这个;只是生产 App Engine 网站。 (虽然我的开发服务器上的数据略有不同;我相信它们都适合标准的 1MB 内存缓存大小,而生产数据更大。)
  • 当我在开发和生产管理面板上搜索“entitylist”时,App Engine 告诉我“没有这样的键”。然而,根据显示的 memcache 的总大小(这是我实现 memcache 的唯一地方),似乎有东西正在被缓存。

谁能指出我应该查看或修复的方向?

【问题讨论】:

标签: python google-app-engine memcached


【解决方案1】:

您是否有一些旧的 MyModel 实例与 MyModel 的当前定义不匹配?如果存在无法识别或缺失的属性,您可能会在酸洗时出错。

【讨论】:

  • 嗨,dragonx。很有意思;我怎么知道是不是这样?
  • 尝试捕获KeyError并打印日志中的序列化字符串,然后您可以看看是怎么回事。
猜你喜欢
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多