Skip to content

Commit 515ad35

Browse files
committed
allow passing popen_kwargs
1 parent cc2c17d commit 515ad35

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

executor/engine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from .core import Engine, EngineSetting
22
from .job import LocalJob, ThreadJob, ProcessJob
33

4-
__version__ = '0.2.3'
4+
__version__ = '0.2.4'
55

66
__all__ = [
77
'Engine', 'EngineSetting',

executor/engine/job/extend/subprocess.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
import shlex
32
import typing as T
43
import subprocess as subp
54
from pathlib import Path
@@ -23,6 +22,7 @@ def SubprocessJob(
2322
wait_time_delta: float = 0.01,
2423
redirect_out_err: bool = False,
2524
target_dir: str = "$current_dir",
25+
popen_kwargs: T.Optional[T.Dict[str, T.Any]] = None,
2626
**attrs
2727
):
2828
"""Create a job that runs a subprocess.
@@ -43,6 +43,7 @@ def SubprocessJob(
4343
Use '$cache_dir' to represent the cache directory of the job.
4444
Use '$current_dir' to represent the current directory of the job.
4545
Default is '$current_dir'.
46+
popen_kwargs: The keyword arguments for subprocess.Popen.
4647
**attrs: Other attributes of the job.
4748
"""
4849
class _SubprocessJob(base_class): # type: ignore
@@ -107,21 +108,29 @@ def record_command():
107108
with open(path_sh, 'w') as f:
108109
f.write(cmd + "\n")
109110

111+
pkwargs = popen_kwargs or {}
112+
pkwargs['cwd'] = target_dir
113+
110114
if self.redirect_out_err:
111115
path_stdout = cache_dir / 'stdout.txt'
112116
path_stderr = cache_dir / 'stderr.txt'
113117

114118
def run_cmd(): # pragma: no cover
115119
runner = ProcessRunner(cmd)
116-
runner.run(cwd=target_dir)
120+
runner.run(**pkwargs)
117121
with open(path_stdout, 'w') as fo, \
118122
open(path_stderr, 'w') as fe:
119123
retcode = runner.write_stream_until_stop(fo, fe)
120124
return retcode
121125
else:
122126
def run_cmd():
123-
p = subp.Popen(shlex.split(cmd), cwd=target_dir)
124-
retcode = p.wait()
127+
runner = ProcessRunner(cmd)
128+
runner.run(
129+
capture_stdout=False,
130+
capture_stderr=False,
131+
**pkwargs
132+
)
133+
retcode = runner.proc.wait()
125134
return retcode
126135

127136
def func():

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmd2func>=0.1.6
1+
cmd2func>=0.1.9
22
funcdesc>=0.1.2
33
loky
44
diskcache

tests/test_subp_job.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,23 @@ async def submit_job():
134134
assert job2.result() == 0
135135

136136
asyncio.run(submit_job())
137+
138+
139+
def test_passing_env():
140+
engine = Engine()
141+
142+
async def submit_job():
143+
import os
144+
env = os.environ.copy()
145+
env["A"] = "2"
146+
job = SubprocessJob(
147+
"python -c 'import os; print(os.getenv(\"A\"))'",
148+
popen_kwargs={"env": env},
149+
redirect_out_err=True
150+
)
151+
await engine.submit_async(job)
152+
await job.join()
153+
with open(job.cache_dir / 'stdout.txt') as f:
154+
assert f.read().strip() == '2'
155+
156+
asyncio.run(submit_job())

0 commit comments

Comments
 (0)