summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Fink2017-12-18 19:08:32 +0100
committerDennis Fink2017-12-18 19:08:32 +0100
commited4abfac084dc8f85a7d23ee86084045b7fa28f4 (patch)
tree69ee375d3e91c15d7f315aeb6d197fd32c24bcf8
parent2cd46141cd87249404d3a90db5abb92e134a5df8 (diff)
downloadpatternutils-ed4abfac084dc8f85a7d23ee86084045b7fa28f4.tar.gz
patternutils-ed4abfac084dc8f85a7d23ee86084045b7fa28f4.zip
Add multiprocess support in targs
-rw-r--r--patternutils/commands/targs.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/patternutils/commands/targs.py b/patternutils/commands/targs.py
index bfacbfc..51f5527 100644
--- a/patternutils/commands/targs.py
+++ b/patternutils/commands/targs.py
@@ -1,11 +1,15 @@
+import functools
import json
import multiprocessing
+import os
import shlex
import subprocess
from typing import (
Union,
List,
+ Dict,
+ Callable,
)
import click
@@ -13,6 +17,16 @@ import click
import patternutils
+def run_command(data: Dict[str, str], render: Callable[..., str], shell: bool=False) -> None:
+ c = render(**data)
+ if not shell:
+ prepared_command = shlex.split(c)
+ else:
+ prepared_command = c
+
+ subprocess.run(prepared_command, shell=shell)
+
+
@click.command(context_settings={'help_option_names': ('-h', '--help', '-?')})
@click.option('--read-from', default='-', type=click.File('r'))
@click.option('-s', '--shell', is_flag=True, default=False,
@@ -21,6 +35,7 @@ import patternutils
help='select the template engine to use')
@click.option('-v', '--verbose', is_flag=True, default=False,
help='explain what is being done')
+@click.option('-P', '--max-procs', default=1, type=click.IntRange(min=0, clamp=True))
@click.version_option()
@click.argument('command')
def targs(
@@ -28,6 +43,7 @@ def targs(
read_from: click.File,
shell: bool,
template_engine: str,
+ max_procs: int,
verbose: bool,
) -> None:
@@ -44,14 +60,15 @@ def targs(
click.secho('jinja2 not installed!', fg='red', err=True)
raise SystemExit
- for r in data:
- c = render(**r)
- if not shell:
- prepared_command = shlex.split(c) # type: Union[str, List[str]]
- else:
- prepared_command = c
+ max_procs = os.cpu_count() * 2 if max_procs == 0 else max_procs
- subprocess.run(prepared_command, shell=shell)
+ patched_run_command = functools.partial(run_command, render=render, shell=shell)
+ if max_procs == 1:
+ for r in data:
+ patched_run_command(r)
+ else:
+ with multiprocessing.Pool(max_procs) as p:
+ p.map(patched_run_command, data)
if __name__ == '__main__':