-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnproc.py
48 lines (42 loc) · 1.16 KB
/
nproc.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
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 20-11-2024
Last update: 20-11-2024
Version: 1.0
Description: The nproc command from GNU coreutils in Python3.
Example of use: python3 nproc.py --all
'''
import os
import sys
import multiprocessing
def get_cpu_count(ignore=False):
"""
Get number of processing units available
Args:
ignore (bool): Ignore number of restricted CPUs
"""
try:
if ignore:
# Total number of CPUs in the system
return os.cpu_count()
else:
# Number of processing units available to the process
return len(os.sched_getaffinity(0))
except Exception:
# Fallback to multiprocessing method
return multiprocessing.cpu_count()
def main():
# Parse command-line arguments
ignore = False
if len(sys.argv) > 1:
if sys.argv[1] == '--all':
ignore = True
else:
print("Usage: nproc [--all]", file=sys.stderr)
sys.exit(1)
# Print number of processing units
print(get_cpu_count(ignore))
if __name__ == "__main__":
main()