liveBook · Manning

Pickle

As explained in Fast Python: High performance techniques for large datasets

Pickle is Python’s standard-library facility for serializing Python objects to byte streams and reconstructing them from those streams.

Pickle serializes Python data into bytes and reconstructs data from those bytes. It commonly carries objects between processes, but it does not transfer code. section 3.1.3

Account for these practical limits when choosing pickle for communication:

  • Lambdas cannot be pickled with the default configuration, so a process pool cannot transfer them as-is; rewriting a lambda as a named function fixes the demonstrated case. section 3.4.1
  • Foreign-library objects may need separate handling; file pointers, database connections, and sockets may be impossible to transfer or require extra care. section 3.4.1
  • Serialization and interprocess communication can be slow enough to undermine multiprocessing when data volumes are large. section 3.4.1

Sending data over a socket

A synchronous client pickles its data, sends the byte length and payload over TCP, and uses a separate mechanism to transfer code. The example highlights that framing and compact, fast encoding may require handling outside pickle. section 3.1.3

<Future at 0x7f1ffff104c0 state=finished raised PicklingError>
Can't pickle <function <lambda> at 0x7f2000131ca0>: attribute lookup
  <lambda> on __main__ failed

The failed future shows the pickling error raised when the process pool tries to transfer a lambda. section 3.4.1

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Shows serialization and reconstruction, then details multiprocessing limits including lambda failures and transfer overhead.
Examples
Uses a TCP client payload and a process-pool lambda failure to illustrate serialization in practice.
What you can do after
Apply pickle to move data through sockets or process pools, and recognize cases requiring separate handling.
How it connects
Ties pickle to multiprocessing, concurrent.futures, sockets, and marshal for code transfer.

Also explained in

Compare how 3 books explain pickle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.