Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pickle protocol to be overridden #4012

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions distributed/distributed-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,18 @@ properties:
type: boolean
description: Enter Python Debugger on scheduling error

pickle:
type: object
description: |
Configuration for pickle serialization
properties:
protocol:
type:
- integer
- "null"
description:
The protocol version to use with pickle

rmm:
type: object
description: |
Expand Down
2 changes: 2 additions & 0 deletions distributed/distributed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ distributed:
log-length: 10000 # default length of logs to keep in memory
log-format: '%(name)s - %(levelname)s - %(message)s'
pdb-on-err: False # enter debug mode on scheduling error
pickle:
protocol: null # specify the pickle protocol to use
Comment on lines +173 to +174
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just placed this at the global level as it is not really worker or scheduler specific. Not picky on where it lives though.

rmm:
pool-size: null
ucx:
Expand Down
8 changes: 5 additions & 3 deletions distributed/protocol/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

import cloudpickle
import dask

if sys.version_info < (3, 8):
try:
Expand Down Expand Up @@ -33,16 +34,17 @@ def _always_use_pickle_for(x):
return False


def dumps(x, *, buffer_callback=None):
def dumps(x, protocol=None, *, buffer_callback=None):
""" Manage between cloudpickle and pickle

1. Try pickle
2. If it is short then check if it contains __main__
3. If it is long, then first check type, then check __main__
"""
protocol = protocol or dask.config.get("pickle.protocol") or HIGHEST_PROTOCOL
buffers = []
dump_kwargs = {"protocol": HIGHEST_PROTOCOL}
if HIGHEST_PROTOCOL >= 5 and buffer_callback is not None:
dump_kwargs = {"protocol": protocol}
if protocol >= 5 and buffer_callback is not None:
dump_kwargs["buffer_callback"] = buffers.append
try:
buffers.clear()
Expand Down
7 changes: 7 additions & 0 deletions distributed/protocol/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def test_pickle_data():
assert deserialize(*serialize(d, serializers=("pickle",))) == d


def test_pickle_protocol():
data = {"int": 1, "float": 2, "unicode": "abc", "bytes": b"def", "set": set()}
for p in range(HIGHEST_PROTOCOL):
assert loads(dumps(data, p)) == data
assert deserialize(*serialize(data, serializers=("pickle",))) == data


def test_pickle_out_of_band():
class MemoryviewHolder:
def __init__(self, mv):
Expand Down