def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. if PY3: # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res else: pickler = Pickler(1) pickler.fast = 1 # Only CPython's cPickle supports dumping # and returning in one operation: # return pickler.dump(args, 1) # For PyPy we must return the value; fortunately this # works the same on CPython and is no more expensive pickler.dump(args) return pickler.getvalue()
def fast_encode(): # Only use in cases where you *know* the data contains only basic # Python objects pickler = Pickler(1) pickler.fast = 1 dump = pickler.dump def fast_encode(*args): return dump(args, 1) return fast_encode
def __init__(self): self.file = tempfile.TemporaryFile(suffix=".tbuf") self.lock = Lock() self.closed = 0 self.count = 0 self.size = 0 self.blobs = [] # It's safe to use a fast pickler because the only objects # stored are builtin types -- strings or None. self.pickler = Pickler(self.file, 1) self.pickler.fast = 1
def __init__(self, connection_generation): self.connection_generation = connection_generation self.file = tempfile.TemporaryFile(suffix=".tbuf") self.count = 0 self.size = 0 self.blobs = [] # It's safe to use a fast pickler because the only objects # stored are builtin types -- strings or None. self.pickler = Pickler(self.file, 1) self.pickler.fast = 1 self.server_resolved = set() # {oid} self.client_resolved = {} # {oid -> buffer_record_number} self.exception = None
def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res
def __init__(self): self.file = tempfile.TemporaryFile(suffix=".comit-log") self.pickler = Pickler(self.file, 1) self.pickler.fast = 1 self.stores = 0