Show More
@@ -118,49 +118,25 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 | |
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. |
|
|||
143 |
|
124 | |||
144 | If it can't find a sensible answer, it returns 1 (though an error *may* make |
|
125 | Return the effective number of CPUs in the system as an integer. | |
145 | it return a large positive number that's actually incorrect). |
|
|||
146 | """ |
|
|||
147 |
|
|
126 | ||
148 | # Many thanks to the Parallel Python project (http://www.parallelpython.com) |
|
127 | This cross-platform function makes an attempt at finding the total number of | |
149 | # for the names of the keys we needed to look up for this function. This |
|
128 | available CPUs in the system, as returned by various underlying system and | |
150 | # code was inspired by their equivalent function. |
|
129 | python calls. | |
151 |
|
|
130 | ||
152 | ncpufuncs = {'Linux':_num_cpus_unix, |
|
131 | If it can't find a sensible answer, it returns 1 (though an error *may* make | |
153 | 'Darwin':_num_cpus_darwin, |
|
132 | it return a large positive number that's actually incorrect). | |
154 | 'Windows':_num_cpus_windows |
|
133 | """ | |
155 | } |
|
134 | import warnings | |
156 |
|
||||
157 | ncpufunc = ncpufuncs.get(platform.system(), |
|
|||
158 | # default to unix version (Solaris, AIX, etc) |
|
|||
159 | _num_cpus_unix) |
|
|||
160 |
|
135 | |||
161 | try: |
|
136 | warnings.warn( | |
162 | ncpus = max(1,int(ncpufunc())) |
|
137 | "`num_cpus` is deprecated since IPython 8.0. Use `os.cpu_count` instead.", | |
163 | except: |
|
138 | DeprecationWarning, | |
164 | ncpus = 1 |
|
139 | stacklevel=2, | |
165 | return ncpus |
|
140 | ) | |
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