Skip to content

Commit d560320

Browse files
authored
Add parallel compile from pybind11 (#8990)
2 parents 5e26d2f + 23ed906 commit d560320

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

.ci/requirements-mypy.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ipython
55
numpy
66
packaging
77
pyarrow-stubs
8+
pybind11
89
pytest
910
sphinx
1011
types-atheris

docs/installation/building-from-source.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,9 @@ Build options
276276

277277
* Config setting: ``-C parallel=n``. Can also be given
278278
with environment variable: ``MAX_CONCURRENCY=n``. Pillow can use
279-
multiprocessing to build the extension. Setting ``-C parallel=n``
279+
multiprocessing to build the extensions. Setting ``-C parallel=n``
280280
sets the number of CPUs to use to ``n``, or can disable parallel building by
281-
using a setting of 1. By default, it uses 4 CPUs, or if 4 are not
282-
available, as many as are present.
281+
using a setting of 1. By default, it uses as many CPUs as are present.
283282

284283
* Config settings: ``-C zlib=disable``, ``-C jpeg=disable``,
285284
``-C tiff=disable``, ``-C freetype=disable``, ``-C raqm=disable``,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[build-system]
22
build-backend = "backend"
33
requires = [
4+
"pybind11",
45
"setuptools>=77",
56
]
67
backend-path = [

setup.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
import warnings
1818
from collections.abc import Iterator
1919

20+
from pybind11.setup_helpers import ParallelCompile
2021
from setuptools import Extension, setup
2122
from setuptools.command.build_ext import build_ext
2223

24+
configuration: dict[str, list[str]] = {}
25+
26+
# parse configuration from _custom_build/backend.py
27+
while sys.argv[-1].startswith("--pillow-configuration="):
28+
_, key, value = sys.argv.pop().split("=", 2)
29+
configuration.setdefault(key, []).append(value)
30+
31+
default = int(configuration.get("parallel", ["0"])[-1])
32+
ParallelCompile("MAX_CONCURRENCY", default).install()
33+
2334

2435
def get_version() -> str:
2536
version_file = "src/PIL/_version.py"
2637
with open(version_file, encoding="utf-8") as f:
2738
return f.read().split('"')[1]
2839

2940

30-
configuration: dict[str, list[str]] = {}
31-
32-
3341
PILLOW_VERSION = get_version()
3442
AVIF_ROOT = None
3543
FREETYPE_ROOT = None
@@ -386,9 +394,7 @@ def finalize_options(self) -> None:
386394
cpu_count = os.cpu_count()
387395
if cpu_count is not None:
388396
try:
389-
self.parallel = int(
390-
os.environ.get("MAX_CONCURRENCY", min(4, cpu_count))
391-
)
397+
self.parallel = int(os.environ.get("MAX_CONCURRENCY", cpu_count))
392398
except TypeError:
393399
pass
394400
for x in self.feature:
@@ -1083,11 +1089,6 @@ def debug_build() -> bool:
10831089
]
10841090

10851091

1086-
# parse configuration from _custom_build/backend.py
1087-
while sys.argv[-1].startswith("--pillow-configuration="):
1088-
_, key, value = sys.argv.pop().split("=", 2)
1089-
configuration.setdefault(key, []).append(value)
1090-
10911092
try:
10921093
setup(
10931094
cmdclass={"build_ext": pil_build_ext},

0 commit comments

Comments
 (0)