Skip to content

Commit 1e0cd04

Browse files
authored
Merge pull request #23 from pyper-dev/dev
generalize the check for invalid multiprocess targets
2 parents fafcef1 + 3520669 commit 1e0cd04

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: src/pyper/_core/task.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import inspect
5+
import pickle
56
from typing import Callable, Dict, Optional, Tuple
67

78

@@ -53,13 +54,12 @@ def __init__(
5354
if self.is_async:
5455
raise ValueError("multiprocess cannot be True for an async task")
5556

56-
# The function needs to be globally accessible to be multiprocessed
57-
# This excludes objects like lambdas and closures
58-
# We capture these cases to throw a clear error message
59-
module = inspect.getmodule(func)
60-
if module is None or getattr(module, func.__name__, None) is not func:
61-
raise RuntimeError(f"{func} cannot be multiprocessed because it is not globally accessible"
62-
f" -- it must be a globally defined object accessible by the name {func.__name__}")
57+
# The function must be picklable
58+
try:
59+
pickle.dumps(func)
60+
except (pickle.PicklingError, AttributeError):
61+
raise RuntimeError(f"{func} cannot be pickled and so cannot be multiprocessed"
62+
f" -- ensure that the function is globally accessible and that its definition has not changed") from None
6363

6464
self.func = func if bind is None else functools.partial(func, *bind[0], **bind[1])
6565
self.branch = branch

0 commit comments

Comments
 (0)