Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH 2/3] multiprocessing.eclass: Introduce get_nproc() to get no of CPUs
Date: Tue, 13 Dec 2016 09:39:22
Message-Id: 20161213093616.2354-3-mgorny@gentoo.org
In Reply to: [gentoo-dev] [PATCH] multiprocessing.eclass: Improvements for wider use by "Michał Górny"
1 Introduce get_nproc(), a portable 'nproc' wrapper. It uses either
2 'nproc' or a fallback Python multiprocessing module call to attempt to
3 determine the number of available processing units.
4
5 This can be used e.g. to determine a safe number of jobs to run when
6 MAKEOPTS specifies unlimited --jobs and the build system in question
7 does not support --load-average.
8 ---
9 eclass/multiprocessing.eclass | 25 +++++++++++++++++++++++++
10 1 file changed, 25 insertions(+)
11
12 diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass
13 index 5a5fe9acb56a..0d241cdc15b6 100644
14 --- a/eclass/multiprocessing.eclass
15 +++ b/eclass/multiprocessing.eclass
16 @@ -53,6 +53,31 @@ bashpid() {
17 sh -c 'echo ${PPID}'
18 }
19
20 +# @FUNCTION: get_nproc
21 +# @USAGE: [${fallback:-1}]
22 +# @DESCRIPTION:
23 +# Attempt to figure out the number of processing units available.
24 +# If the value can not be determined, prints the provided fallback
25 +# instead. If no fallback is provided, defaults to 1.
26 +get_nproc() {
27 + local nproc
28 +
29 + if type -P nproc &>/dev/null; then
30 + # GNU
31 + nproc=$(nproc)
32 + elif type -P python &>/dev/null; then
33 + # fallback to python2.6+
34 + # note: this may fail (raise NotImplementedError)
35 + nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
36 + fi
37 +
38 + if [[ -n ${nproc} ]]; then
39 + echo "${nproc}"
40 + else
41 + echo "${1:-1}"
42 + fi
43 +}
44 +
45 # @FUNCTION: makeopts_jobs
46 # @USAGE: [${MAKEOPTS}]
47 # @DESCRIPTION:
48 --
49 2.11.0

Replies