Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96550e0

Browse files
authored
Merge pull request #13280 from Kojoley/deprecate-num_cpus
Deprecate `utils.sysinfo.num_cpus`
2 parents 418efa8 + fc34990 commit 96550e0

2 files changed

Lines changed: 21 additions & 39 deletions

File tree

IPython/utils/sysinfo.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -118,49 +118,25 @@ def sys_info():
118118
"""
119119
return pprint.pformat(get_sys_info())
120120

121-
def _num_cpus_unix():
122-
"""Return the number of active CPUs on a Unix system."""
123-
return os.sysconf("SC_NPROCESSORS_ONLN")
124-
125-
126-
def _num_cpus_darwin():
127-
"""Return the number of active CPUs on a Darwin system."""
128-
p = subprocess.Popen(['sysctl','-n','hw.ncpu'],stdout=subprocess.PIPE)
129-
return p.stdout.read()
130-
131-
132-
def _num_cpus_windows():
133-
"""Return the number of active CPUs on a Windows system."""
134-
return os.environ.get("NUMBER_OF_PROCESSORS")
135-
136121

137122
def num_cpus():
138-
"""Return the effective number of CPUs in the system as an integer.
139-
140-
This cross-platform function makes an attempt at finding the total number of
141-
available CPUs in the system, as returned by various underlying system and
142-
python calls.
123+
"""DEPRECATED
143124
144-
If it can't find a sensible answer, it returns 1 (though an error *may* make
145-
it return a large positive number that's actually incorrect).
146-
"""
125+
Return the effective number of CPUs in the system as an integer.
147126
148-
# Many thanks to the Parallel Python project (http://www.parallelpython.com)
149-
# for the names of the keys we needed to look up for this function. This
150-
# code was inspired by their equivalent function.
127+
This cross-platform function makes an attempt at finding the total number of
128+
available CPUs in the system, as returned by various underlying system and
129+
python calls.
151130
152-
ncpufuncs = {'Linux':_num_cpus_unix,
153-
'Darwin':_num_cpus_darwin,
154-
'Windows':_num_cpus_windows
155-
}
156-
157-
ncpufunc = ncpufuncs.get(platform.system(),
158-
# default to unix version (Solaris, AIX, etc)
159-
_num_cpus_unix)
131+
If it can't find a sensible answer, it returns 1 (though an error *may* make
132+
it return a large positive number that's actually incorrect).
133+
"""
134+
import warnings
160135

161-
try:
162-
ncpus = max(1,int(ncpufunc()))
163-
except:
164-
ncpus = 1
165-
return ncpus
136+
warnings.warn(
137+
"`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
138+
DeprecationWarning,
139+
stacklevel=2,
140+
)
166141

142+
return os.cpu_count() or 1

IPython/utils/tests/test_sysinfo.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Distributed under the terms of the Modified BSD License.
66

77
import json
8+
import pytest
89

910
from IPython.utils import sysinfo
1011

@@ -14,3 +15,8 @@ def test_json_getsysinfo():
1415
test that it is easily jsonable and don't return bytes somewhere.
1516
"""
1617
json.dumps(sysinfo.get_sys_info())
18+
19+
20+
def test_num_cpus():
21+
with pytest.deprecated_call():
22+
sysinfo.num_cpus()

0 commit comments

Comments
 (0)