-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxargs_concurrent.py
69 lines (65 loc) · 2.38 KB
/
xargs_concurrent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import argparse, collections, json, multiprocessing.pool, os, shlex, subprocess, sys
processes = None
command = None
creationflags = subprocess.CREATE_NEW_CONSOLE
def parse_arguments():
global command, creationflags, processes
parser = argparse.ArgumentParser(
description=
"This script takes each line in standard input, appends it to the "
"specified command, and runs it. Once all commands have completed, "
"the number of occurrences of each return value is printed in JSON "
"format."
)
parser.add_argument(
"--num-processes", "-n",
type=int,
default=os.cpu_count(),
metavar="N",
help=
"By default, the number of parallel processes that are spawned how "
"many parallel processes to spawn is limited to the number of CPUs "
"of the computer. Use this switch to override this number."
)
parser.add_argument(
"--no-new-console", "-p",
action="store_true",
help=
"By default, each command is spawned in its own window. Specify this "
"switch to use only this console window."
)
parser.add_argument(
"command",
nargs=argparse.REMAINDER,
help=
"Specify the command to run and its initial arguments. Each line in "
"standard input is appended to this text, and the whole string is "
"treated as the command."
)
parsed = parser.parse_args()
processes = parsed.num_processes
if parsed.no_new_console:
creationflags = 0
command = parsed.command
if not len(command):
parser.error("no command was specified")
def start_command(command_arg):
global command, creationflags
return subprocess.Popen(command + shlex.split(command_arg), creationflags=creationflags).wait()
if __name__ == "__main__":
parse_arguments()
try:
with multiprocessing.pool.ThreadPool(processes) as pool:
return_value_counts = collections.Counter(pool.imap_unordered(start_command, sys.stdin))
except OSError as e:
print(e)
else:
# Print a JSON object of the number of times that each return value occurred.
json.dump(
return_value_counts,
sys.stdout,
sort_keys=True,
indent=4,
separators=(',', ': ')
)