Skip to content

Commit dbba594

Browse files
committed
Auto merge of #111067 - albertlarsan68:fix-multiprocessing-x-py, r=jyn514
Make x.py work again in most (all?) cases Fixes #111046. Wrap all of x.py in `if __name__ == '__main__':` to avoid problems with `multiprocessing` Make the pool sizing better
2 parents 6db1e5e + 3011949 commit dbba594

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/bootstrap/bootstrap.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ def platform_is_win32():
2828
else:
2929
EXE_SUFFIX = ""
3030

31+
def get_cpus():
32+
if hasattr(os, "sched_getaffinity"):
33+
return len(os.sched_getaffinity(0))
34+
if hasattr(os, "cpu_count"):
35+
cpus = os.cpu_count()
36+
if cpus is not None:
37+
return cpus
38+
try:
39+
return cpu_count()
40+
except NotImplementedError:
41+
return 1
42+
43+
44+
3145
def get(base, url, path, checksums, verbose=False):
3246
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
3347
temp_path = temp_file.name
@@ -540,11 +554,15 @@ def download_toolchain(self):
540554

541555
# Unpack the tarballs in parallle.
542556
# In Python 2.7, Pool cannot be used as a context manager.
543-
p = Pool(min(len(tarballs_download_info), cpu_count()))
557+
pool_size = min(len(tarballs_download_info), get_cpus())
558+
if self.verbose:
559+
print('Choosing a pool size of', pool_size, 'for the unpacking of the tarballs')
560+
p = Pool(pool_size)
544561
try:
545562
p.map(unpack_component, tarballs_download_info)
546563
finally:
547564
p.close()
565+
p.join()
548566

549567
if self.should_fix_bins_and_dylibs():
550568
self.fix_bin_or_dylib("{}/bin/cargo".format(bin_root))

x.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@
44

55
# This file is only a "symlink" to bootstrap.py, all logic should go there.
66

7-
import os
8-
import sys
7+
# Parts of `bootstrap.py` use the `multiprocessing` module, so this entry point
8+
# must use the normal `if __name__ == '__main__':` convention to avoid problems.
9+
if __name__ == '__main__':
10+
import os
11+
import sys
912

10-
# If this is python2, check if python3 is available and re-execute with that
11-
# interpreter. Only python3 allows downloading CI LLVM.
12-
#
13-
# This matters if someone's system `python` is python2.
14-
if sys.version_info.major < 3:
15-
try:
16-
os.execvp("py", ["py", "-3"] + sys.argv)
17-
except OSError:
13+
# If this is python2, check if python3 is available and re-execute with that
14+
# interpreter. Only python3 allows downloading CI LLVM.
15+
#
16+
# This matters if someone's system `python` is python2.
17+
if sys.version_info.major < 3:
1818
try:
19-
os.execvp("python3", ["python3"] + sys.argv)
19+
os.execvp("py", ["py", "-3"] + sys.argv)
2020
except OSError:
21-
# Python 3 isn't available, fall back to python 2
22-
pass
21+
try:
22+
os.execvp("python3", ["python3"] + sys.argv)
23+
except OSError:
24+
# Python 3 isn't available, fall back to python 2
25+
pass
2326

24-
rust_dir = os.path.dirname(os.path.abspath(__file__))
25-
# For the import below, have Python search in src/bootstrap first.
26-
sys.path.insert(0, os.path.join(rust_dir, "src", "bootstrap"))
27+
rust_dir = os.path.dirname(os.path.abspath(__file__))
28+
# For the import below, have Python search in src/bootstrap first.
29+
sys.path.insert(0, os.path.join(rust_dir, "src", "bootstrap"))
2730

28-
import bootstrap
29-
bootstrap.main()
31+
import bootstrap
32+
bootstrap.main()

0 commit comments

Comments
 (0)