Mercurial > hg
comparison mercurial/worker.py @ 26063:d29859cfcfc2
worker: use multiprocessing to find cpu count
The multiprocessing package was added in Python 2.6.
The implementation of worker.countcpus() is very similar to
multiprocessing.cpu_count(). Ditch our one-off code.
multiprocessing does result in a number of imports. However,
the lazy importer ensures that we don't import anything until
cpu_count() is called. Furthermore, if we are doing something
with multiple cores, chances are the time of that operation
will dwarf the import time, so module bloat isn't a concern
here.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 25 May 2015 13:10:38 -0700 |
parents | 2d76f8a2d831 |
children | c0501c26b05c |
comparison
equal
deleted
inserted
replaced
26062:7154a4a08b96 | 26063:d29859cfcfc2 |
---|---|
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 from __future__ import absolute_import | 8 from __future__ import absolute_import |
9 | 9 |
10 import errno | 10 import errno |
11 import multiprocessing | |
11 import os | 12 import os |
12 import signal | 13 import signal |
13 import sys | 14 import sys |
14 import threading | 15 import threading |
15 | 16 |
16 from .i18n import _ | 17 from .i18n import _ |
17 from . import util | 18 from . import util |
18 | 19 |
19 def countcpus(): | 20 def countcpus(): |
20 '''try to count the number of CPUs on the system''' | 21 '''try to count the number of CPUs on the system''' |
21 | |
22 # posix | |
23 try: | 22 try: |
24 n = int(os.sysconf('SC_NPROCESSORS_ONLN')) | 23 return multiprocessing.cpu_count() |
25 if n > 0: | 24 except NotImplementedError: |
26 return n | 25 return 1 |
27 except (AttributeError, ValueError): | |
28 pass | |
29 | |
30 # windows | |
31 try: | |
32 n = int(os.environ['NUMBER_OF_PROCESSORS']) | |
33 if n > 0: | |
34 return n | |
35 except (KeyError, ValueError): | |
36 pass | |
37 | |
38 return 1 | |
39 | 26 |
40 def _numworkers(ui): | 27 def _numworkers(ui): |
41 s = ui.config('worker', 'numcpus') | 28 s = ui.config('worker', 'numcpus') |
42 if s: | 29 if s: |
43 try: | 30 try: |