Skip to content

Commit dc8af08

Browse files
committed
nproc: Add nproc.py
1 parent 96ab461 commit dc8af08

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/nproc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python3
2+
3+
import os
4+
from optparse import OptionParser
5+
6+
7+
def nproc(opts):
8+
n_cpus = os.cpu_count() if opts.all else os.process_cpu_count()
9+
10+
print(max(n_cpus - opts.ignore, 1))
11+
12+
13+
if __name__ == "__main__":
14+
parser = OptionParser(
15+
usage="Usage: %prog [OPTION]...",
16+
description="Print the number of processing units available to the process.",
17+
add_help_option=False,
18+
)
19+
parser.add_option("--help", action="help", help="show usage information and exit")
20+
21+
parser.add_option(
22+
"--all",
23+
action="store_true",
24+
help="print the total number of installed processors",
25+
)
26+
27+
parser.add_option(
28+
"--ignore",
29+
type="int",
30+
default=0,
31+
metavar="N",
32+
help="exclude up to N processors if possible",
33+
)
34+
35+
opts, args = parser.parse_args()
36+
37+
if args:
38+
parser.error(f"extra operand '{args[0]}'")
39+
40+
nproc(opts)

0 commit comments

Comments
 (0)