Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time


.. currentmodule:: distributed.protocol.serialize

When we communicate data between computers we first convert that data into a sequence of bytes that can be communicated across the network. Choices made in serialization can affect performance and security.

The standard Python solution to this, Pickle, is often but not always the right solution. Dask uses a number of different serialization schemes in different situations. These are extensible to allow users to control in sensitive situations and also to enable library developers to plug in more performant serialization solutions.

This document first describes Dask's default solution for serialization and then discusses ways to control and extend that serialiation.


There are three kinds of messages passed through the Dask network:

  1. Small administrative messages like "Worker A has finished task X" or "I'm running out of memory". These are always serialized with msgpack.
  2. Movement of program data, such as Numpy arrays and Pandas dataframes. This uses a combination of pickle and custom serializers and is the topic of the next section
  3. Computational tasks like f(x) that are defined and serialized on client processes and deserialized and run on worker processes. These are serialized using a fixed scheme decided on by those libraries. Today this is a combination of pickle and cloudpickle.

Serialization families


For the movement of program data (item 2 above) we can use a few different families of serializers. By default the following families are built in:

  1. Pickle and cloudpickle
  2. Msgpack
  3. Custom per-type serializers that come with Dask for the special serialization of important classes of data like Numpy arrays

You can choose which families you want to use to serialize data and to deserialize data when you create a Client

from dask.distributed import Client
client = Client('tcp://scheduler-address:8786',
                serializers=['dask', 'pickle'],
                deserializers=['dask', 'msgpack'])

This can be useful if, for example, you are sensitive about receiving Pickle-serialized data for security reasons.

Dask uses the serializers ['dask', 'pickle'] by default, trying to use dask custom serializers (described below) if they work and then falling back to pickle/cloudpickle.


These families can be extended by creating two functions, dumps and loads, which return and consume a msgpack-encodable header, and a list of byte-like objects. These must then be included in the distributed.protocol.serialize dictionary with an appropriate name. Here is the definition of pickle_dumps and pickle_loads to serve as an example.

import pickle

def pickle_dumps(x):
    header = {'serializer': 'pickle'}
    frames = [pickle.dumps(x)]
    return header, frames

def pickle_loads(header, frames):
    if len(frames) > 1:  # this may be cut up for network reasons
        frame = ''.join(frames)
        frame = frames[0]
    return pickle.loads(frame)

from distributed.protocol.serialize import register_serialization_family
register_serialization_family('pickle', pickle_dumps, pickle_loads)

After this the name 'pickle' can be used in the serializers= and deserializers= keywords in Client and other parts of Dask.

Communication Context


This is an experimental feature and may change without notice

Dask :doc:`Comms <communications>` can provide additional context to serialization family functions if they provide a context= keyword. This allows serialization to behave differently according to how it is being used.

def my_dumps(x, context=None):
    if context and 'recipient' in context:
        # check if we're sending to the same host or not

The context depends on the kind of communication. For example when sending over TCP, the address of the sender (us) and the recipient are available in a dictionary.

>>> context
{'sender': 'tcp://', 'recipient': 'tcp://'}

Other comms may provide other information.

Dask Serialization Family


Dask maintains its own custom serialization family that special cases a few important types, like Numpy arrays. These serializers either operate more efficiently than Pickle, or serialize types that Pickle can not handle.

You don't need to do anything special to use this family of serializers. It is on by default (along with pickle). Note that Dask custom serializers may use pickle internally in some cases. It should not be considered more secure.


.. autosummary::

As with serialization families in general, the Dask family in particular is also extensible. This is a good way to support custom serialization of a single type of object. The method is similar, you create serialize and deserialize function that create and consume a header and frames, and then register them with Dask.

class Human:
    def __init__(self, name): = name

from distributed.protocol import dask_serialize, dask_deserialize

def serialize(human: Human) -> Tuple[Dict, List[bytes]]:
    header = {}
    frames = []
    return header, frames

def deserialize(header: Dict, frames: List[bytes]) -> Human:
    return Human(frames[0].decode())

Traverse attributes

.. autosummary::

A common case is that your object just wraps Numpy arrays or other objects that Dask already serializes well. For example, Scikit-Learn estimators mostly surround Numpy arrays with a bit of extra metadata. In these cases you can register your class for custom Dask serialization with the register_generic function.


.. autosummary:: serialize

.. autofunction:: serialize
.. autofunction:: deserialize
.. autofunction:: dask_serialize
.. autofunction:: dask_deserialize
.. autofunction:: register_generic