如何在 Google AppEngine 上获取 python 对象的大小(以字节为单位)?
我需要计算一些 python 对象的大小,这样我就可以将它们分解并将它们存储在内存缓存中,而不会达到大小限制。
GAE 环境中的 python 对象上似乎不存在“sizeof()”,并且 sys.getsizeof() 也不可用。
GAE 本身显然正在幕后检查尺寸以强制执行这些限制。关于如何实现这一目标有什么想法吗?谢谢。
I need to compute the sizes of some python objects, so I can break them up and store them in memcache without hitting size limits.
'sizeof()' doesn't seem to be present on python objects in the GAE environment and sys.getsizeof() is also unavailable.
GAE itself is clearly checking sizes behind the scenes to enforce the limits. Any ideas for how to accomplish this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
memcache
在内部总是使用pickle
并存储结果字符串,因此您可以使用len(pickle.dumps(yourobject, -1))
检查。请注意,sys.getsizeof(需要 2.6 或更高版本,这就是 GAE 上缺少它的原因)根本不会真正帮助您:因为对象的序列化 pickle 的大小可能与内存中对象的大小有很大不同,如您所见(所以我想您应该感谢 GAE 没有提供 sizeof,这会让您误入歧途;-)。
memcache
internally and invariably usespickle
and stores the resulting string, so you can check withlen(pickle.dumps(yourobject, -1))
. Note that sys.getsizeof (which requires 2.6 or better, which is why it's missing on GAE) would not really help you at all:since the size of a serialized pickle of the object can be quite different from the size of the object in memory, as you can see (so I guess you should feel grateful to GAE for not offering sizeof, which would have led you astray;-).