-
-
Notifications
You must be signed in to change notification settings - Fork 335
Multiprocessing support #2815
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
Multiprocessing support #2815
Changes from all commits
4d30312
5dceb04
b1378a7
b558f7f
bf6c79d
4f6f433
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import dataclasses | ||
import json | ||
import math | ||
import multiprocessing as mp | ||
import pickle | ||
import re | ||
import sys | ||
from itertools import accumulate | ||
from typing import TYPE_CHECKING, Any, Literal | ||
from unittest import mock | ||
|
@@ -1382,3 +1384,39 @@ def test_roundtrip_numcodecs() -> None: | |
metadata = root["test"].metadata.to_dict() | ||
expected = (*filters, BYTES_CODEC, *compressors) | ||
assert metadata["codecs"] == expected | ||
|
||
|
||
def _index_array(arr: Array, index: Any) -> Any: | ||
return arr[index] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method", | ||
[ | ||
pytest.param( | ||
"fork", | ||
marks=pytest.mark.skipif( | ||
sys.platform in ("win32", "darwin"), reason="fork not supported on Windows or OSX" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because windows should only perform spawn? This decorator is a bit verbose, it would be OK to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer (verbose) parametrization over checking the OS in the test itself. The former gives better feedback in the test summary. |
||
), | ||
), | ||
"spawn", | ||
pytest.param( | ||
"forkserver", | ||
marks=pytest.mark.skipif( | ||
sys.platform == "win32", reason="forkserver not supported on Windows" | ||
), | ||
), | ||
], | ||
) | ||
@pytest.mark.parametrize("store", ["local"], indirect=True) | ||
def test_multiprocessing(store: Store, method: Literal["fork", "spawn", "forkserver"]) -> None: | ||
""" | ||
Test that arrays can be pickled and indexed in child processes | ||
""" | ||
data = np.arange(100) | ||
arr = zarr.create_array(store=store, data=data) | ||
ctx = mp.get_context(method) | ||
pool = ctx.Pool() | ||
|
||
results = pool.starmap(_index_array, [(arr, slice(len(data)))]) | ||
assert all(np.array_equal(r, data) for r in results) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strictly speaking, loop and iothread don't need to be global, since they are mutated in-place.