##// END OF EJS Templates
Deprecate `utils.sysinfo.num_cpus`...
Nikita Kniazev -
Show More
@@ -118,49 +118,25 b' def sys_info():'
118 118 """
119 119 return pprint.pformat(get_sys_info())
120 120
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
136 121
137 122 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
143 124
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.
147 126
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.
151 130
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
160 135
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 )
166 141
142 return os.cpu_count() or 1
@@ -5,6 +5,7 b''
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7 import json
8 import pytest
8 9
9 10 from IPython.utils import sysinfo
10 11
@@ -14,3 +15,8 b' def test_json_getsysinfo():'
14 15 test that it is easily jsonable and don't return bytes somewhere.
15 16 """
16 17 json.dumps(sysinfo.get_sys_info())
18
19
20 def test_num_cpus():
21 with pytest.deprecated_call():
22 sysinfo.num_cpus()
General Comments 0
You need to be logged in to leave comments. Login now