Skip to content

Commit 30c2ec8

Browse files
authored
Use the default multiprocessing pool configuration. (#2596)
Previously we were forcing use of fork for all OSes to try to juice performance at the cost of running into threading issues at some point when things got more complex. They may have reached that point with CI lockups happening on macOS. Revert to the default pool setup to attempt to fix macOS hangs. To support this, also propagate any CLI global `Variables` adjustments to subprocesses. Previously these were only propagated for the current process; now the adjustments propagate through the environment so that subprocesses see the same adjustments. In particular, this ensures subprocesses use the same `PEX_ROOT` when a fallback has to be used to work around lack of write perms.
1 parent 7e3c248 commit 30c2ec8

14 files changed

+52
-44
lines changed

pex/commands/command.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pex import pex_warnings
1818
from pex.argparse import HandleBoolAction
1919
from pex.cache import access as cache_access
20-
from pex.common import safe_mkdtemp, safe_open
20+
from pex.common import environment_as, safe_mkdtemp, safe_open
2121
from pex.compatibility import shlex_quote
2222
from pex.result import Error, Ok, Result
2323
from pex.typing import TYPE_CHECKING, Generic, cast
@@ -379,7 +379,7 @@ def warn_ignore_pex_root(set_via):
379379
else:
380380
pex_root = options.cache_dir or options.pex_root or ENV.PEX_ROOT
381381

382-
with ENV.patch(PEX_ROOT=pex_root, TMPDIR=tmpdir) as env:
382+
with ENV.patch(PEX_ROOT=pex_root, TMPDIR=tmpdir) as env, environment_as(**env):
383383
cache_access.read_write()
384384
yield env
385385

pex/common.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import time
1818
import zipfile
1919
from collections import defaultdict, namedtuple
20+
from contextlib import contextmanager
2021
from datetime import datetime
2122
from uuid import uuid4
2223
from zipfile import ZipFile, ZipInfo
@@ -917,3 +918,28 @@ def iter_copytree(
917918
if copy_mode is CopyMode.SYMLINK:
918919
# Once we've symlinked the top-level directories and files, we've "copied" everything.
919920
return
921+
922+
923+
@contextmanager
924+
def environment_as(**kwargs):
925+
# type: (**Any) -> Iterator[None]
926+
"""Mutates the `os.environ` for the duration of the context.
927+
928+
Keyword arguments with None values are removed from os.environ (if present) and all other
929+
keyword arguments are added or updated in `os.environ` with the values taken from the
930+
stringification (`str(...)`) of each value.
931+
"""
932+
existing = {key: os.environ.get(key) for key in kwargs}
933+
934+
def adjust_environment(mapping):
935+
for key, value in mapping.items():
936+
if value is not None:
937+
os.environ[key] = str(value)
938+
else:
939+
os.environ.pop(key, None)
940+
941+
adjust_environment(kwargs)
942+
try:
943+
yield
944+
finally:
945+
adjust_environment(existing)

pex/jobs.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from pex.common import pluralize
1818
from pex.compatibility import Queue, cpu_count
1919
from pex.tracer import TRACER
20-
from pex.typing import TYPE_CHECKING, Generic, cast
20+
from pex.typing import TYPE_CHECKING, Generic
2121

2222
if TYPE_CHECKING:
2323
from typing import (
@@ -679,12 +679,8 @@ def join(self):
679679
@contextmanager
680680
def _mp_pool(size):
681681
# type: (int) -> Iterator[Pool]
682-
try:
683-
context = multiprocessing.get_context("fork") # type: ignore[attr-defined]
684-
pool = cast("Pool", context.Pool(processes=size))
685-
except (AttributeError, ValueError):
686-
pool = multiprocessing.Pool(processes=size)
687682

683+
pool = multiprocessing.Pool(processes=size)
688684
try:
689685
yield pool
690686
finally:

testing/__init__.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -674,25 +674,6 @@ def all_python_venvs(system_site_packages=False):
674674
)
675675

676676

677-
@contextmanager
678-
def environment_as(**kwargs):
679-
# type: (**Any) -> Iterator[None]
680-
existing = {key: os.environ.get(key) for key in kwargs}
681-
682-
def adjust_environment(mapping):
683-
for key, value in mapping.items():
684-
if value is not None:
685-
os.environ[key] = str(value)
686-
else:
687-
os.environ.pop(key, None)
688-
689-
adjust_environment(kwargs)
690-
try:
691-
yield
692-
finally:
693-
adjust_environment(existing)
694-
695-
696677
@contextmanager
697678
def pushd(directory):
698679
# type: (Text) -> Iterator[None]

tests/integration/cli/commands/test_cache_prune.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
VenvDirs,
2828
)
2929
from pex.cli.commands.cache.du import DiskUsage
30-
from pex.common import safe_open
30+
from pex.common import environment_as, safe_open
3131
from pex.pep_503 import ProjectName
3232
from pex.pex_info import PexInfo
3333
from pex.pip.version import PipVersion, PipVersionValue
3434
from pex.typing import TYPE_CHECKING
3535
from pex.variables import ENV
36-
from testing import environment_as, run_pex_command
36+
from testing import run_pex_command
3737
from testing.cli import run_pex3
3838
from testing.pytest.tmp import Tempdir
3939

tests/integration/test_integration.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919

2020
from pex import targets
2121
from pex.cache.dirs import CacheDir, InterpreterDir
22-
from pex.common import is_exe, safe_mkdir, safe_open, safe_rmtree, temporary_dir, touch
22+
from pex.common import (
23+
environment_as,
24+
is_exe,
25+
safe_mkdir,
26+
safe_open,
27+
safe_rmtree,
28+
temporary_dir,
29+
touch,
30+
)
2331
from pex.compatibility import WINDOWS, commonpath
2432
from pex.dist_metadata import Distribution, Requirement, is_wheel
2533
from pex.fetcher import URLFetcher
@@ -46,7 +54,6 @@
4654
IntegResults,
4755
built_wheel,
4856
ensure_python_interpreter,
49-
environment_as,
5057
get_dep_dist_names_from_pex,
5158
make_env,
5259
run_pex_command,

tests/integration/test_issue_157.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import pytest
1515
from colors import color # vendor:skip
1616

17+
from pex.common import environment_as
1718
from pex.pex_info import PexInfo
1819
from pex.typing import TYPE_CHECKING
1920
from pex.version import __version__
20-
from testing import IS_PYPY, environment_as, make_env, run_pex_command, scie
21+
from testing import IS_PYPY, make_env, run_pex_command, scie
2122

2223
if TYPE_CHECKING:
2324
from typing import Any, Iterable, Iterator, List, Tuple

tests/resolve/test_target_options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111

1212
import pex.resolve.target_configuration
13+
from pex.common import environment_as
1314
from pex.interpreter import PythonInterpreter
1415
from pex.pep_425 import CompatibilityTags
1516
from pex.pep_508 import MarkerEnvironment
@@ -20,7 +21,7 @@
2021
from pex.targets import CompletePlatform, Targets
2122
from pex.typing import TYPE_CHECKING
2223
from pex.variables import ENV
23-
from testing import IS_MAC, environment_as
24+
from testing import IS_MAC
2425

2526
if TYPE_CHECKING:
2627
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type

tests/test_atomic_directory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import pytest
1010

1111
from pex.atomic_directory import AtomicDirectory, FileLockStyle, _is_bsd_lock, atomic_directory
12-
from pex.common import temporary_dir, touch
12+
from pex.common import environment_as, temporary_dir, touch
1313
from pex.typing import TYPE_CHECKING
14-
from testing import environment_as
1514

1615
try:
1716
from unittest import mock

tests/test_interpreter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pytest
1515

1616
from pex.cache.dirs import InterpreterDir
17-
from pex.common import chmod_plus_x, safe_mkdir, safe_mkdtemp, temporary_dir, touch
17+
from pex.common import chmod_plus_x, environment_as, safe_mkdir, safe_mkdtemp, temporary_dir, touch
1818
from pex.executor import Executor
1919
from pex.interpreter import PythonInterpreter, create_shebang
2020
from pex.jobs import Job
@@ -30,7 +30,6 @@
3030
ensure_python_distribution,
3131
ensure_python_interpreter,
3232
ensure_python_venv,
33-
environment_as,
3433
pushd,
3534
)
3635
from testing.pytest.tmp import TempdirFactory

tests/test_pex.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717

1818
from pex import resolver
19-
from pex.common import safe_mkdir, safe_open, temporary_dir
19+
from pex.common import environment_as, safe_mkdir, safe_open, temporary_dir
2020
from pex.compatibility import PY2, WINDOWS, to_bytes
2121
from pex.dist_metadata import Distribution
2222
from pex.interpreter import PythonIdentity, PythonInterpreter
@@ -33,7 +33,6 @@
3333
PY_VER,
3434
WheelBuilder,
3535
ensure_python_interpreter,
36-
environment_as,
3736
install_wheel,
3837
make_bdist,
3938
run_simple_pex,

tests/test_pip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import pytest
1515

16-
from pex.common import safe_rmtree
16+
from pex.common import environment_as, safe_rmtree
1717
from pex.dist_metadata import Distribution, Requirement
1818
from pex.interpreter import PythonInterpreter
1919
from pex.jobs import Job
@@ -29,7 +29,7 @@
2929
from pex.typing import TYPE_CHECKING
3030
from pex.variables import ENV
3131
from pex.venv.virtualenv import Virtualenv
32-
from testing import IS_LINUX, PY310, ensure_python_interpreter, environment_as
32+
from testing import IS_LINUX, PY310, ensure_python_interpreter
3333
from testing.pytest.tmp import Tempdir
3434

3535
if TYPE_CHECKING:

tests/test_requirements.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from pex.common import safe_open, temporary_dir, touch
11+
from pex.common import environment_as, safe_open, temporary_dir, touch
1212
from pex.dist_metadata import Requirement
1313
from pex.fetcher import URLFetcher
1414
from pex.requirements import (
@@ -30,7 +30,6 @@
3030
)
3131
from pex.third_party.packaging.markers import Marker
3232
from pex.typing import TYPE_CHECKING
33-
from testing import environment_as
3433

3534
if TYPE_CHECKING:
3635
from typing import Any, Iterable, Iterator, List, Optional, Union

tests/test_variables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import pytest
88

99
from pex import pex_warnings
10+
from pex.common import environment_as
1011
from pex.compatibility import PY2
1112
from pex.pex_warnings import PEXWarning
1213
from pex.typing import TYPE_CHECKING
1314
from pex.util import named_temporary_file
1415
from pex.variables import NoValueError, Variables
15-
from testing import environment_as
1616
from testing.pytest.tmp import Tempdir
1717

1818
if TYPE_CHECKING:

0 commit comments

Comments
 (0)