##// END OF EJS Templates
Deprecate `utils.sysinfo.num_cpus`...
Nikita Kniazev -
Show More
@@ -118,24 +118,11 b' def sys_info():'
118 """
118 """
119 return pprint.pformat(get_sys_info())
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 def num_cpus():
122 def num_cpus():
138 """Return the effective number of CPUs in the system as an integer.
123 """DEPRECATED
124
125 Return the effective number of CPUs in the system as an integer.
139
126
140 This cross-platform function makes an attempt at finding the total number of
127 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
128 available CPUs in the system, as returned by various underlying system and
@@ -144,23 +131,12 b' def num_cpus():'
144 If it can't find a sensible answer, it returns 1 (though an error *may* make
131 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).
132 it return a large positive number that's actually incorrect).
146 """
133 """
134 import warnings
147
135
148 # Many thanks to the Parallel Python project (http://www.parallelpython.com)
136 warnings.warn(
149 # for the names of the keys we needed to look up for this function. This
137 "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.",
150 # code was inspired by their equivalent function.
138 DeprecationWarning,
151
139 stacklevel=2,
152 ncpufuncs = {'Linux':_num_cpus_unix,
140 )
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)
160
161 try:
162 ncpus = max(1,int(ncpufunc()))
163 except:
164 ncpus = 1
165 return ncpus
166
141
142 return os.cpu_count() or 1
@@ -5,6 +5,7 b''
5 # Distributed under the terms of the Modified BSD License.
5 # Distributed under the terms of the Modified BSD License.
6
6
7 import json
7 import json
8 import pytest
8
9
9 from IPython.utils import sysinfo
10 from IPython.utils import sysinfo
10
11
@@ -14,3 +15,8 b' def test_json_getsysinfo():'
14 test that it is easily jsonable and don't return bytes somewhere.
15 test that it is easily jsonable and don't return bytes somewhere.
15 """
16 """
16 json.dumps(sysinfo.get_sys_info())
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