Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask-parallel) and
## 0.4.2 - 2024-xx-xx

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

## 0.4.1 - 2024-01-12

Expand Down
34 changes: 20 additions & 14 deletions src/pytask_parallel/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,46 @@

from __future__ import annotations

import enum
import os
from typing import Any

from pytask import hookimpl

from pytask_parallel import execute
from pytask_parallel import processes
from pytask_parallel import threads
from pytask_parallel.backends import ParallelBackend


@hookimpl
def pytask_parse_config(config: dict[str, Any]) -> None:
"""Parse the configuration."""
__tracebackhide__ = True

if config["n_workers"] == "auto":
config["n_workers"] = max(os.cpu_count() - 1, 1)

if (
isinstance(config["parallel_backend"], str)
and config["parallel_backend"] in ParallelBackend._value2member_map_ # noqa: SLF001
):
try:
config["parallel_backend"] = ParallelBackend(config["parallel_backend"])
elif (
isinstance(config["parallel_backend"], enum.Enum)
and config["parallel_backend"] in ParallelBackend
):
pass
else:
msg = f"Invalid value for 'parallel_backend'. Got {config['parallel_backend']}."
raise ValueError(msg)
except ValueError:
msg = (
f"Invalid value for 'parallel_backend'. Got {config['parallel_backend']}. "
f"Choose one of {', '.join([e.value for e in ParallelBackend])}."
)
raise ValueError(msg) from None

config["delay"] = 0.1


@hookimpl
def pytask_post_parse(config: dict[str, Any]) -> None:
"""Disable parallelization if debugging is enabled."""
"""Register the parallel backend if debugging is not enabled."""
if config["pdb"] or config["trace"] or config["dry_run"]:
config["n_workers"] = 1

if config["n_workers"] > 1:
config["pm"].register(execute)
if config["parallel_backend"] == ParallelBackend.THREADS:
config["pm"].register(threads)
else:
config["pm"].register(processes)
Loading