Skip to content

Commit 6102efb

Browse files
committed
added in code from main pydra package
1 parent 193c028 commit 6102efb

File tree

9 files changed

+135
-30
lines changed

9 files changed

+135
-30
lines changed

.github/workflows/ci-cd.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
- name: Install task package
4141
run: |
4242
pip install ${{ matrix.pip-flags }} ".[dev]"
43-
python -c "import pydra.workers.CHANGEME as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
43+
python -c "import pydra.workers.psij as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
4444
python -c "import pydra as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
4545
4646
test:
@@ -61,12 +61,12 @@ jobs:
6161
- name: Install task package
6262
run: |
6363
pip install ".[test]"
64-
python -c "import pydra.workers.CHANGEME as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
64+
python -c "import pydra.workers.psij as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
6565
python -c "import pydra as m; print(f'{m.__name__} {m.__version__} @ {m.__file__}')"
6666
- name: Test with pytest
6767
run: |
68-
pytest -sv --doctest-modules pydra/workers/CHANGEME \
69-
--cov pydra.workers.CHANGEME --cov-report xml
68+
pytest -sv --doctest-modules pydra/workers/psij \
69+
--cov pydra.workers.psij --cov-report xml
7070
- uses: codecov/codecov-action@v3
7171
if: ${{ always() }}
7272

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ dmypy.json
141141
.DS_store
142142

143143
# Generated files
144-
/pydra/workers/CHANGEME/_version.py
144+
/pydra/workers/psij/_version.py

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ workers, within the `pydra.workers.<yourworkerpackagename>` namespace.
88
1. Click on new repo.
99
1. Select this template from the repository template drop down list.
1010
1. Give your repo a name.
11-
1. Once the repo is created and cloned, search for CHANGEME (`grep -rn CHANGEME . `) and
11+
1. Once the repo is created and cloned, search for psij (`grep -rn psij . `) and
1212
replace with appropriate name.
13-
1. Rename the namespace package root directory to replace `CHANGEME` with the name of the package:
14-
* `pydra/workers/CHANGEME`
13+
1. Rename the namespace package root directory to replace `psij` with the name of the package:
14+
* `pydra/workers/psij`
1515
1. Add your new worker class to `pydra/workers/<package-name>/__init__.py`
1616
1. You may want to initialize a [Sphinx] docs directory.
1717
1. Review the workflow in `.github/workflows/pythonpackage.yml`. Testing editable installations

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
# -- Project information -----------------------------------------------------
1919

20-
project = "pydra-workers-CHANGEME"
20+
project = "pydra-workers-psij"
2121
copyright = "2020, Xihe Xie"
2222
author = "Xihe Xie"
2323

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Welcome to pydra-workers-CHANGEME's documentation!
2-
==================================================
1+
Welcome to pydra-workers-psij's documentation!
2+
==============================================
33

44
.. toctree::
55
:maxdepth: 2

pydra/workers/CHANGEME/__init__.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import typing as ty
2+
from pathlib import Path
3+
import cloudpickle as cp
4+
import logging
5+
import attrs
6+
import psij
7+
from pydra.engine.job import Job
8+
from pydra.workers import base
9+
10+
logger = logging.getLogger("pydra.worker")
11+
12+
if ty.TYPE_CHECKING:
13+
from pydra.engine.result import Result
14+
15+
16+
@attrs.define
17+
class Worker(base.Worker):
18+
"""A worker to execute tasks using PSI/J."""
19+
20+
subtype = "local"
21+
22+
def make_spec(self, cmd=None, arg=None):
23+
"""
24+
Create a PSI/J job specification.
25+
26+
Parameters
27+
----------
28+
cmd : str, optional
29+
Executable command. Defaults to None.
30+
arg : list, optional
31+
List of arguments. Defaults to None.
32+
33+
Returns
34+
-------
35+
psij.JobDef
36+
PSI/J job specification.
37+
"""
38+
spec = psij.JobSpec()
39+
spec.executable = cmd
40+
spec.arguments = arg
41+
42+
return spec
43+
44+
def make_job(self, spec, attributes):
45+
"""
46+
Create a PSI/J job.
47+
48+
Parameters
49+
----------
50+
task : psij.JobDef
51+
PSI/J job specification.
52+
attributes : any
53+
Job attributes.
54+
55+
Returns
56+
-------
57+
psij.Job
58+
PSI/J job.
59+
"""
60+
job = psij.Job()
61+
job.spec = spec
62+
return job
63+
64+
async def run(
65+
self,
66+
job: Job[base.TaskType],
67+
rerun: bool = False,
68+
) -> "Result":
69+
"""
70+
Run a job (coroutine wrapper).
71+
72+
Raises
73+
------
74+
Exception
75+
If stderr is not empty.
76+
77+
Returns
78+
-------
79+
None
80+
"""
81+
jex = psij.JobExecutor.get_instance(self.subtype)
82+
absolute_path = Path(__file__).parent
83+
84+
cache_dir = job.cache_dir
85+
file_path = cache_dir / "runnable_function.pkl"
86+
with open(file_path, "wb") as file:
87+
cp.dump(job.run, file)
88+
func_path = absolute_path / "run_pickled.py"
89+
spec = self.make_spec("python", [func_path, file_path])
90+
91+
if rerun:
92+
spec.arguments.append("--rerun")
93+
94+
spec.stdout_path = cache_dir / "demo.stdout"
95+
spec.stderr_path = cache_dir / "demo.stderr"
96+
97+
psij_job = self.make_job(spec, None)
98+
jex.submit(psij_job)
99+
psij_job.wait()
100+
101+
if spec.stderr_path.stat().st_size > 0:
102+
with open(spec.stderr_path, "r") as stderr_file:
103+
stderr_contents = stderr_file.read()
104+
raise Exception(
105+
f"stderr_path '{spec.stderr_path}' is not empty. Contents:\n{stderr_contents}"
106+
)
107+
108+
return job.result()
109+
110+
def close(self):
111+
"""Finalize the internal pool of tasks."""
112+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pydra.workers import psij_local
2+
3+
4+
class Worker(psij_local.Worker):
5+
"""A worker to execute tasks using PSI/J using SLURM."""
6+
7+
subtype = "slurm"

pyproject.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ requires = ["hatchling", "hatch-vcs"]
33
build-backend = "hatchling.build"
44

55
[project]
6-
name = "pydra-workers-CHANGEME"
7-
description = "Pydra workers package for CHANGEME"
6+
name = "pydra-workers-psij"
7+
description = "Pydra workers package for psij"
88
readme = "README.md"
99
requires-python = ">=3.11"
1010
dependencies = [
@@ -30,6 +30,7 @@ dynamic = ["version"]
3030
dev = [
3131
"black",
3232
"pre-commit",
33+
"types-cloudpickle"
3334
]
3435
doc = [
3536
"packaging",
@@ -52,11 +53,11 @@ test = [
5253
source = "vcs"
5354

5455
[tool.hatch.build.hooks.vcs]
55-
version-file = "pydra/workers/CHANGEME/_version.py"
56+
version-file = "pydra/workers/psij/_version.py"
5657

5758
[tool.hatch.build.targets.wheel]
5859
packages = ["pydra"]
59-
include-only = ["pydra/workers/CHANGEME"]
60+
include-only = ["pydra/workers/psij"]
6061

6162
[tool.black]
6263
target-version = ["py38"]

0 commit comments

Comments
 (0)