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

Revert pickle change #8456

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 4 additions & 9 deletions distributed/protocol/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,14 @@
buffers.clear()
pickler.dump(x)
result = f.getvalue()

if not _always_use_pickle_for(x) and (
if b"__main__" in result or (
CLOUDPICKLE_GE_20
and getattr(inspect.getmodule(x), "__name__", None)
in cloudpickle.list_registry_pickle_by_value()
or (
len(result) < 1000
# Do this very last since it's expensive
and b"__main__" in result
)
):
buffers.clear()
result = cloudpickle.dumps(x, **dump_kwargs)
if len(result) < 1000 or not _always_use_pickle_for(x):
buffers.clear()
result = cloudpickle.dumps(x, **dump_kwargs)

Check warning on line 77 in distributed/protocol/pickle.py

View check run for this annotation

Codecov / codecov/patch

distributed/protocol/pickle.py#L75-L77

Added lines #L75 - L77 were not covered by tests
except Exception:
try:
buffers.clear()
Expand Down
23 changes: 22 additions & 1 deletion distributed/protocol/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
loads,
)
from distributed.protocol.serialize import dask_deserialize, dask_serialize
from distributed.utils_test import save_sys_modules
from distributed.utils_test import popen, save_sys_modules


class MemoryviewHolder:
Expand Down Expand Up @@ -278,3 +278,24 @@ def test_nopickle_nested():
finally:
del dask_serialize._lookup[NoPickle]
del dask_deserialize._lookup[NoPickle]


@pytest.mark.slow()
def test_pickle_functions_in_main(tmp_path):
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding this!

script = """
from dask.distributed import Client
if __name__ == "__main__":
with Client(n_workers=1) as client:
def func(df):
return (df + 5)
client.submit(func, 5).result()
print("success")
"""
with open(tmp_path / "script.py", mode="w") as f:
f.write(script)
with popen([sys.executable, tmp_path / "script.py"], capture_output=True) as proc:
out, _ = proc.communicate(timeout=60)

lines = out.decode("utf-8").split("\n")

assert "success" in lines
Loading