Skip to content

Commit 1a2dd82

Browse files
authored
bpo-36786: Run compileall in parallel during "make install" (pythonGH-13078)
1 parent c981ad1 commit 1a2dd82

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

Doc/library/compileall.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ Public functions
158158
The argument *workers* specifies how many workers are used to
159159
compile files in parallel. The default is to not use multiple workers.
160160
If the platform can't use multiple workers and *workers* argument is given,
161-
then sequential compilation will be used as a fallback. If *workers* is
161+
then sequential compilation will be used as a fallback. If *workers*
162+
is 0, the number of cores in the system is used. If *workers* is
162163
lower than ``0``, a :exc:`ValueError` will be raised.
163164

164165
*invalidation_mode* should be a member of the
@@ -184,6 +185,9 @@ Public functions
184185
.. versionchanged:: 3.7
185186
The *invalidation_mode* parameter was added.
186187

188+
.. versionchanged:: 3.8
189+
Setting *workers* to 0 now chooses the optimal number of cores.
190+
187191
.. function:: compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, invalidation_mode=py_compile.PycInvalidationMode.TIMESTAMP)
188192

189193
Compile the file with path *fullname*. Return a true value if the file

Lib/compileall.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,20 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
6767
invalidation_mode: how the up-to-dateness of the pyc will be checked
6868
"""
6969
ProcessPoolExecutor = None
70-
if workers is not None:
71-
if workers < 0:
72-
raise ValueError('workers must be greater or equal to 0')
73-
elif workers != 1:
74-
try:
75-
# Only import when needed, as low resource platforms may
76-
# fail to import it
77-
from concurrent.futures import ProcessPoolExecutor
78-
except ImportError:
79-
workers = 1
70+
if workers < 0:
71+
raise ValueError('workers must be greater or equal to 0')
72+
if workers != 1:
73+
try:
74+
# Only import when needed, as low resource platforms may
75+
# fail to import it
76+
from concurrent.futures import ProcessPoolExecutor
77+
except ImportError:
78+
workers = 1
8079
files = _walk_dir(dir, quiet=quiet, maxlevels=maxlevels,
8180
ddir=ddir)
8281
success = True
83-
if workers is not None and workers != 1 and ProcessPoolExecutor is not None:
82+
if workers != 1 and ProcessPoolExecutor is not None:
83+
# If workers == 0, let ProcessPoolExecutor choose
8484
workers = workers or None
8585
with ProcessPoolExecutor(max_workers=workers) as executor:
8686
results = executor.map(partial(compile_file,
@@ -290,9 +290,6 @@ def main():
290290
print("Error reading file list {}".format(args.flist))
291291
return False
292292

293-
if args.workers is not None:
294-
args.workers = args.workers or None
295-
296293
if args.invalidation_mode:
297294
ivl_mode = args.invalidation_mode.replace('-', '_').upper()
298295
invalidation_mode = py_compile.PycInvalidationMode[ivl_mode]

Lib/test/test_compileall.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def test_workers_available_cores(self, compile_dir):
575575
new=[sys.executable, self.directory, "-j0"]):
576576
compileall.main()
577577
self.assertTrue(compile_dir.called)
578-
self.assertEqual(compile_dir.call_args[-1]['workers'], None)
578+
self.assertEqual(compile_dir.call_args[-1]['workers'], 0)
579579

580580

581581
class CommmandLineTestsWithSourceEpoch(CommandLineTestsBase,

Makefile.pre.in

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,30 +1432,30 @@ libinstall: build_all $(srcdir)/Modules/xxmodule.c
14321432
fi
14331433
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14341434
$(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
1435-
-d $(LIBDEST) -f \
1435+
-j0 -d $(LIBDEST) -f \
14361436
-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
14371437
$(DESTDIR)$(LIBDEST)
14381438
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14391439
$(PYTHON_FOR_BUILD) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
1440-
-d $(LIBDEST) -f \
1440+
-j0 -d $(LIBDEST) -f \
14411441
-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
14421442
$(DESTDIR)$(LIBDEST)
14431443
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14441444
$(PYTHON_FOR_BUILD) -Wi -OO $(DESTDIR)$(LIBDEST)/compileall.py \
1445-
-d $(LIBDEST) -f \
1445+
-j0 -d $(LIBDEST) -f \
14461446
-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
14471447
$(DESTDIR)$(LIBDEST)
14481448
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14491449
$(PYTHON_FOR_BUILD) -Wi $(DESTDIR)$(LIBDEST)/compileall.py \
1450-
-d $(LIBDEST)/site-packages -f \
1450+
-j0 -d $(LIBDEST)/site-packages -f \
14511451
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
14521452
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14531453
$(PYTHON_FOR_BUILD) -Wi -O $(DESTDIR)$(LIBDEST)/compileall.py \
1454-
-d $(LIBDEST)/site-packages -f \
1454+
-j0 -d $(LIBDEST)/site-packages -f \
14551455
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
14561456
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14571457
$(PYTHON_FOR_BUILD) -Wi -OO $(DESTDIR)$(LIBDEST)/compileall.py \
1458-
-d $(LIBDEST)/site-packages -f \
1458+
-j0 -d $(LIBDEST)/site-packages -f \
14591459
-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
14601460
-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
14611461
$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/Grammar.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"make install" now runs compileall in parallel.

0 commit comments

Comments
 (0)