如何在Google AppEngine上获取以字节为单位的python对象的大小? [英] How to get the size of a python object in bytes on Google AppEngine?

查看:108
本文介绍了如何在Google AppEngine上获取以字节为单位的python对象的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一些python对象的大小,所以我可以将它们分解并将它们存储在memcache中,而不会触及大小限制。

' sizeof ()'似乎不在GAE环境中的python对象上,并且sys.getsizeof()也不可用。

GAE本身显然会检查幕后的大小以强制执行限制。任何想法如何实现这一目标?感谢。

解决方案

memcache 内部和总是使用 pickle 并存储结果字符串,因此您可以使用 len(pickle.dumps(yourobject,-1))进行检查。请注意,sys.getsizeof(它需要2.6或更高版本,这就是为什么它在GAE上丢失的原因)根本无法帮助您:

 >>> import sys 
>>> sys.getsizeof(23)
12
>>> import pickle
>>> len(pickle.dumps(23,-1))
5

你可以看到,对象的序列化pickle可能与内存中对象的大小非常不同(所以我想你应该感谢GAE提供sizeof,而这会导致你误入歧途; - )。

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.

解决方案

memcache internally and invariably uses pickle and stores the resulting string, so you can check with len(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:

>>> import sys
>>> sys.getsizeof(23)
12
>>> import pickle
>>> len(pickle.dumps(23, -1))
5

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;-).

这篇关于如何在Google AppEngine上获取以字节为单位的python对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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