How to get the size of a python object in bytes on Google AppEngine?

£可爱£侵袭症+ 提交于 2019-12-19 03:18:47

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/3432402/how-to-get-the-size-of-a-python-object-in-bytes-on-google-appengine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!