Skip to content

Commit cede64d

Browse files
author
bmartinn
committed
CI pep8, flake8, mypy, py27
1 parent 9eb6111 commit cede64d

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

news/8187.feature

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
Run the wheel install in a multiprocessing Pool, this has x1.5 speedup factor when installing cached packages.
2-
Packages that could not be installed (exception raised), will be installed serially once the Pool is done.
3-
If multiprocessing.Pool is not supported by the platform, fall-back to serial installation.
1+
Run the wheel install in a multiprocessing Pool, this has x1.5 speedup factor
2+
when installing cached packages. Packages that could not be installed
3+
(exception raised), will be installed serially once the Pool is done.
4+
If multiprocessing.Pool is not supported by the platform,
5+
fall-back to serial installation.

src/pip/_internal/req/__init__.py

+36-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import absolute_import
55

66
import logging
7+
from functools import partial
78

89
from pip._internal.utils.logging import indent_log
910
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -13,12 +14,12 @@
1314
from .req_set import RequirementSet
1415

1516
try:
16-
from multiprocessing.pool import Pool
17+
from multiprocessing.pool import Pool # noqa
1718
except ImportError: # Platform-specific: No multiprocessing available
18-
Pool = None
19+
Pool = None # type: ignore
1920

2021
if MYPY_CHECK_RUNNING:
21-
from typing import Any, List, Sequence
22+
from typing import Any, List, Sequence, Optional
2223

2324
__all__ = [
2425
"RequirementSet", "InstallRequirement",
@@ -59,64 +60,71 @@ def install_given_reqs(
5960
)
6061

6162
# pre allocate installed package names
62-
installed = [None] * len(to_install)
63+
installed = \
64+
[None] * len(to_install) # type: List[Optional[InstallationResult]]
65+
6366
install_args = [install_options, global_options, args, kwargs]
6467

65-
if Pool is not None:
68+
if to_install and Pool is not None:
6669
# first let's try to install in parallel, if we fail we do it by order.
6770
pool = Pool()
6871
try:
69-
pool_result = pool.starmap_async(__single_install, [(install_args, r) for r in to_install])
72+
pool_result = pool.map_async(
73+
partial(__single_install, install_args), to_install)
7074
# python 2.7 timeout=None will not catch KeyboardInterrupt
7175
installed = pool_result.get(timeout=999999)
7276
except (KeyboardInterrupt, SystemExit):
7377
pool.terminate()
7478
raise
75-
except Exception:
76-
# we will reinstall sequentially
77-
pass
7879
pool.close()
7980
pool.join()
8081

8182
with indent_log():
8283
for i, requirement in enumerate(to_install):
8384
if installed[i] is None:
84-
installed[i] = __single_install(install_args, requirement, allow_raise=True)
85+
installed[i] = __single_install(
86+
install_args, requirement, allow_raise=True)
8587

8688
return [i for i in installed if i is not None]
8789

8890

89-
def __single_install(args, a_requirement, allow_raise=False):
90-
if a_requirement.should_reinstall:
91-
logger.info('Attempting uninstall: %s', a_requirement.name)
91+
def __single_install(
92+
install_args, # type: List[Any]
93+
requirement, # type: InstallRequirement
94+
allow_raise=False # type: bool
95+
):
96+
# type: (...) -> Optional[InstallationResult]
97+
"""
98+
Install a single requirement, returns InstallationResult
99+
100+
(to be called per requirement, either in parallel or serially)
101+
"""
102+
if requirement.should_reinstall:
103+
logger.info('Attempting uninstall: %s', requirement.name)
92104
with indent_log():
93-
uninstalled_pathset = a_requirement.uninstall(
105+
uninstalled_pathset = requirement.uninstall(
94106
auto_confirm=True
95107
)
96108
try:
97-
a_requirement.install(
98-
args[0], # install_options,
99-
args[1], # global_options,
100-
*args[2], # *args,
101-
**args[3] # **kwargs
109+
requirement.install(
110+
install_args[0], # install_options,
111+
install_args[1], # global_options,
112+
*install_args[2], # *args,
113+
**install_args[3] # **kwargs
102114
)
103115
except Exception:
104-
should_rollback = (
105-
a_requirement.should_reinstall and
106-
not a_requirement.install_succeeded
107-
)
116+
should_rollback = (requirement.should_reinstall and
117+
not requirement.install_succeeded)
108118
# if install did not succeed, rollback previous uninstall
109119
if should_rollback:
110120
uninstalled_pathset.rollback()
111121
if allow_raise:
112122
raise
113123
else:
114-
should_commit = (
115-
a_requirement.should_reinstall and
116-
a_requirement.install_succeeded
117-
)
124+
should_commit = (requirement.should_reinstall and
125+
requirement.install_succeeded)
118126
if should_commit:
119127
uninstalled_pathset.commit()
120-
return InstallationResult(a_requirement.name)
128+
return InstallationResult(requirement.name)
121129

122130
return None

0 commit comments

Comments
 (0)