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

Skip to content

Commit 37cfb0a

Browse files
committed
Issue #17914: Use os.cpu_count() instead of multiprocessing.cpu_count() where
applicable.
1 parent c723da3 commit 37cfb0a

4 files changed

Lines changed: 7 additions & 13 deletions

File tree

Doc/library/multiprocessing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ with the :class:`Pool` class.
16641664
callbacks and has a parallel map implementation.
16651665

16661666
*processes* is the number of worker processes to use. If *processes* is
1667-
``None`` then the number returned by :func:`cpu_count` is used. If
1667+
``None`` then the number returned by :func:`os.cpu_count` is used. If
16681668
*initializer* is not ``None`` then each worker process will call
16691669
``initializer(*initargs)`` when it starts.
16701670

Lib/concurrent/futures/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def __init__(self, max_workers=None):
331331
_check_system_limits()
332332

333333
if max_workers is None:
334-
self._max_workers = multiprocessing.cpu_count()
334+
self._max_workers = os.cpu_count() or 1
335335
else:
336336
self._max_workers = max_workers
337337

Lib/multiprocessing/pool.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
import queue
1818
import itertools
1919
import collections
20+
import os
2021
import time
2122
import traceback
2223

23-
from multiprocessing import Process, cpu_count, TimeoutError
24+
from multiprocessing import Process, TimeoutError
2425
from multiprocessing.util import Finalize, debug
2526

2627
#
@@ -147,10 +148,7 @@ def __init__(self, processes=None, initializer=None, initargs=(),
147148
self._initargs = initargs
148149

149150
if processes is None:
150-
try:
151-
processes = cpu_count()
152-
except NotImplementedError:
153-
processes = 1
151+
processes = os.cpu_count() or 1
154152
if processes < 1:
155153
raise ValueError("Number of processes must be at least 1")
156154

Lib/test/regrtest.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
508508
elif o in ('-j', '--multiprocess'):
509509
use_mp = int(a)
510510
if use_mp <= 0:
511-
try:
512-
import multiprocessing
513-
# Use all cores + extras for tests that like to sleep
514-
use_mp = 2 + multiprocessing.cpu_count()
515-
except (ImportError, NotImplementedError):
516-
use_mp = 3
511+
# Use all cores + extras for tests that like to sleep
512+
use_mp = 2 + (os.cpu_count() or 1)
517513
if use_mp == 1:
518514
use_mp = None
519515
elif o == '--header':

0 commit comments

Comments
 (0)