Python 3 - Can pickle handle byte objects larger than 4GB?

前端 未结 7 1159
我寻月下人不归
我寻月下人不归 2020-12-01 03:54

Based on this comment and the referenced documentation, Pickle 4.0+ from Python 3.4+ should be able to pickle byte objects larger than 4 GB.

However, using python 3

7条回答
  •  暖寄归人
    2020-12-01 04:38

    Reading a file by 2GB chunks takes twice as much memory as needed if bytes concatenation is performed, my approach to loading pickles is based on bytearray:

    class MacOSFile(object):
        def __init__(self, f):
            self.f = f
    
        def __getattr__(self, item):
            return getattr(self.f, item)
    
        def read(self, n):
            if n >= (1 << 31):
                buffer = bytearray(n)
                pos = 0
                while pos < n:
                    size = min(n - pos, 1 << 31 - 1)
                    chunk = self.f.read(size)
                    buffer[pos:pos + size] = chunk
                    pos += size
                return buffer
            return self.f.read(n)
    

    Usage:

    with open("/path", "rb") as fin:
        obj = pickle.load(MacOSFile(fin))
    

提交回复
热议问题