PicklingError:无法腌制< class ...&gt ;:它与GAE中的...不是同一对象 [英] PicklingError: Can't pickle <class ...>: it's not the same object as ... in GAE

查看:73
本文介绍了PicklingError:无法腌制< class ...&gt ;:它与GAE中的...不是同一对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GAE Python应用中的这一行代码中出现了PicklingError:

I'm getting a PicklingError from this line of code in my GAE Python app:

deferred.defer(email_voters_begin, ekey, voter_list)

这三个参数是:

  • email_voters_begin-Python函数,例如,函数email_voters_begin位于0x1035d4488
  • ekey-我定义的实体的键,例如,打印为agdvcGF2b3Rlcg4LEghFbGVjdGlvbhgCDA
  • voter_list-我定义的对象列表,例如[models.Voter object at 0x103d3d310,...]

当这行作为我的测试的一部分(使用webtest和nosegae)执行时,出现以下错误:

When this line executes as part of my tests (with webtest and nosegae), I get the following error:

Traceback (most recent call last):
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/tasks.py", line 29, in init_voters_and_send_email
    deferred.defer(email_voters_begin, ekey, voter_list)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/deferred/deferred.py", line 249, in defer
    pickled = serialize(obj, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/deferred/deferred.py", line 221, in serialize
    return pickle.dumps(curried, protocol=pickle.HIGHEST_PROTOCOL)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1374, in dumps
    Pickler(file, protocol).dump(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  [...]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 396, in save_reduce
    save(cls)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 753, in save_global
    (obj, module, name))
PicklingError: Can't pickle <class 'google.appengine.ext.blobstore.blobstore.BlobInfo'>: it's not the same object as google.appengine.ext.blobstore.blobstore.BlobInfo

请注意,传入deferred.defer()Voter实体不具有BlobReference属性,但是Voter实体的确对另一个具有BlobReference属性的实体具有ReferenceProperty.我不认为任何BlobInfo对象都会成为泡菜的一部分,但是错误提示其中包含了一个.

Note that the Voter entities passed in deferred.defer() do not have a BlobReference property, but the Voter entities do have a ReferenceProperty to another entity that does have a BlobReference property. I wouldn't think that any BlobInfo objects would be part of the pickle but the error suggests one is being included.

当我使用开发服务器在浏览器窗口中运行相同的代码时,不会发生此错误.

This error does not occur when I run the same code from a browser window using the dev server.

我很困惑如何调试它,任何想法都将不胜感激.

I'm stumped as how to debug this and any ideas would be greatly appreciated.

推荐答案

这很可能发生,因为您已经在至少一个Voter对象上检索了ReferenceProperty.取消引用ReferenceProperty后,模型实例将对其进行缓存.酸洗也会酸洗所有缓存的对象,因此它会尝试序列化Voter实例,所引用的实例及其BlobInfo.

This is likely occurring because you've retrieved the ReferenceProperty on at least one of the Voter objects. Once you dereference a ReferenceProperty, the model instance caches it. Pickling pickles any cached objects too, so it attempts to serialize the Voter instance, the referenced instance, and its BlobInfo.

通常,正如Skirmantas指出的那样,将模型实例传递给延迟实例通常是个坏主意.在可能的情况下,发送密钥,如果没有,则将实例序列化到协议缓冲区,然后发送给它们.

In general, as Skirmantas points out, it's usually a bad idea to pass model instances to deferred. Where possible, send keys, and if not, serialize the instances to Protocol Buffers and send those instead.

这篇关于PicklingError:无法腌制&lt; class ...&gt ;:它与GAE中的...不是同一对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆