Skip to content

Commit 69b982f

Browse files
authored
Restructure the package. (#89)
1 parent 11be482 commit 69b982f

File tree

7 files changed

+404
-387
lines changed

7 files changed

+404
-387
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask-parallel) and
88
## 0.4.2 - 2024-xx-xx
99

1010
- {pull}`85` simplifies code since loky is a dependency.
11+
- {pull}`89` restructures the package.
1112

1213
## 0.4.1 - 2024-01-12
1314

src/pytask_parallel/config.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@
22

33
from __future__ import annotations
44

5-
import enum
65
import os
76
from typing import Any
87

98
from pytask import hookimpl
109

10+
from pytask_parallel import execute
11+
from pytask_parallel import processes
12+
from pytask_parallel import threads
1113
from pytask_parallel.backends import ParallelBackend
1214

1315

1416
@hookimpl
1517
def pytask_parse_config(config: dict[str, Any]) -> None:
1618
"""Parse the configuration."""
19+
__tracebackhide__ = True
20+
1721
if config["n_workers"] == "auto":
1822
config["n_workers"] = max(os.cpu_count() - 1, 1)
1923

20-
if (
21-
isinstance(config["parallel_backend"], str)
22-
and config["parallel_backend"] in ParallelBackend._value2member_map_ # noqa: SLF001
23-
):
24+
try:
2425
config["parallel_backend"] = ParallelBackend(config["parallel_backend"])
25-
elif (
26-
isinstance(config["parallel_backend"], enum.Enum)
27-
and config["parallel_backend"] in ParallelBackend
28-
):
29-
pass
30-
else:
31-
msg = f"Invalid value for 'parallel_backend'. Got {config['parallel_backend']}."
32-
raise ValueError(msg)
26+
except ValueError:
27+
msg = (
28+
f"Invalid value for 'parallel_backend'. Got {config['parallel_backend']}. "
29+
f"Choose one of {', '.join([e.value for e in ParallelBackend])}."
30+
)
31+
raise ValueError(msg) from None
3332

3433
config["delay"] = 0.1
3534

3635

3736
@hookimpl
3837
def pytask_post_parse(config: dict[str, Any]) -> None:
39-
"""Disable parallelization if debugging is enabled."""
38+
"""Register the parallel backend if debugging is not enabled."""
4039
if config["pdb"] or config["trace"] or config["dry_run"]:
4140
config["n_workers"] = 1
41+
42+
if config["n_workers"] > 1:
43+
config["pm"].register(execute)
44+
if config["parallel_backend"] == ParallelBackend.THREADS:
45+
config["pm"].register(threads)
46+
else:
47+
config["pm"].register(processes)

0 commit comments

Comments
 (0)