Skip to content

Commit b99ca39

Browse files
Merge pull request #85024 from charles-zablit/charles-zablit/update-checkout/re-enable-quiet-3.9
[update-checkout] enable quiet mode on Python 3.9 and inferior
2 parents 5ea66e6 + 8c64530 commit b99ca39

File tree

2 files changed

+47
-65
lines changed

2 files changed

+47
-65
lines changed

utils/update_checkout/update_checkout/parallel_runner.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,71 @@
1-
from multiprocessing.managers import ListProxy, ValueProxy
21
import sys
3-
from multiprocessing import cpu_count, Manager
2+
from multiprocessing import cpu_count
43
import time
5-
from typing import Callable, List, Any, Union
4+
from typing import Callable, List, Any, Tuple, Union
65
from threading import Lock, Thread, Event
76
from concurrent.futures import ThreadPoolExecutor
87
import shutil
98

109
from .runner_arguments import RunnerArguments, AdditionalSwiftSourcesArguments
1110

1211

12+
class TaskTracker:
13+
_running_tasks: List[str]
14+
_done_task_counter: int
15+
_lock: Lock
16+
17+
def __init__(self):
18+
self._running_tasks = []
19+
self._done_task_counter = 0
20+
self._lock = Lock()
21+
22+
def mark_task_as_running(self, task_name: str):
23+
self._lock.acquire()
24+
self._running_tasks.append(task_name)
25+
self._lock.release()
26+
27+
def mark_task_as_done(self, task_name: str):
28+
self._lock.acquire()
29+
if task_name in self._running_tasks:
30+
self._running_tasks.remove(task_name)
31+
self._done_task_counter += 1
32+
self._lock.release()
33+
34+
def status(self) -> Tuple[List[str], int]:
35+
self._lock.acquire()
36+
running_tasks_str = ", ".join(self.running_tasks)
37+
done_tasks = self.done_task_counter
38+
self._lock.release()
39+
return running_tasks_str, done_tasks
40+
41+
@property
42+
def running_tasks(self) -> List[str]:
43+
return self._running_tasks
44+
45+
@property
46+
def done_task_counter(self) -> int:
47+
return self._done_task_counter
48+
49+
1350
class MonitoredFunction:
1451
def __init__(
1552
self,
1653
fn: Callable,
17-
running_tasks: ListProxy,
18-
updated_repos: ValueProxy,
19-
lock: Lock,
54+
task_tracker: TaskTracker,
2055
):
2156
self.fn = fn
22-
self.running_tasks = running_tasks
23-
self.updated_repos = updated_repos
24-
self._lock = lock
57+
self._task_tracker = task_tracker
2558

2659
def __call__(self, *args: Union[RunnerArguments, AdditionalSwiftSourcesArguments]):
2760
task_name = args[0].repo_name
28-
self.running_tasks.append(task_name)
61+
self._task_tracker.mark_task_as_running(task_name)
2962
result = None
3063
try:
3164
result = self.fn(*args)
3265
except Exception as e:
3366
print(e)
3467
finally:
35-
self._lock.acquire()
36-
if task_name in self.running_tasks:
37-
self.running_tasks.remove(task_name)
38-
self.updated_repos.set(self.updated_repos.get() + 1)
39-
self._lock.release()
68+
self._task_tracker.mark_task_as_done(task_name)
4069
return result
4170

4271

@@ -61,13 +90,8 @@ def __init__(
6190
self._stop_event = Event()
6291
self._verbose = pool_args[0].verbose
6392
if not self._verbose:
64-
manager = Manager()
65-
self._lock = manager.Lock()
66-
self._running_tasks = manager.list()
67-
self._updated_repos = manager.Value("i", 0)
68-
self._monitored_fn = MonitoredFunction(
69-
self._fn, self._running_tasks, self._updated_repos, self._lock
70-
)
93+
self._task_tracker = TaskTracker()
94+
self._monitored_fn = MonitoredFunction(self._fn, self._task_tracker)
7195

7296
def run(self) -> List[Any]:
7397
print(f"Running ``{self._fn.__name__}`` with up to {self._n_threads} processes.")
@@ -86,12 +110,7 @@ def run(self) -> List[Any]:
86110
def _monitor(self):
87111
last_output = ""
88112
while not self._stop_event.is_set():
89-
self._lock.acquire()
90-
current = list(self._running_tasks)
91-
current_line = ", ".join(current)
92-
updated_repos = self._updated_repos.get()
93-
self._lock.release()
94-
113+
current_line, updated_repos = self._task_tracker.status()
95114
if current_line != last_output:
96115
truncated = f"{self._output_prefix} [{updated_repos}/{self._nb_repos}] ({current_line})"
97116
if len(truncated) > self._terminal_width:

utils/update_checkout/update_checkout/update_checkout.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import os
1414
import platform
1515
import re
16-
import tempfile
1716
import sys
1817
import traceback
1918
from multiprocessing import freeze_support
@@ -28,28 +27,6 @@
2827
SCRIPT_FILE = os.path.abspath(__file__)
2928
SCRIPT_DIR = os.path.dirname(SCRIPT_FILE)
3029

31-
def is_unix_sock_path_too_long() -> bool:
32-
"""Check if the unix socket_path exceeds the 104 limit (108 on Linux).
33-
34-
The multiprocessing module creates a socket in the TEMPDIR folder. The
35-
socket path should not exceed:
36-
- 104 bytes on macOS
37-
- 108 bytes on Linux (https://www.man7.org/linux/man-pages/man7/unix.7.html)
38-
39-
Returns:
40-
bool: Whether the socket path exceeds the limit. Always False on Windows.
41-
"""
42-
43-
if os.name != "posix":
44-
return False
45-
46-
MAX_UNIX_SOCKET_PATH = 104
47-
# `tempfile.mktemp` is deprecated yet that's what the multiprocessing
48-
# module uses internally: https://github.com/python/cpython/blob/c4e7d245d61ac4547ecf3362b28f64fc00aa88c0/Lib/multiprocessing/connection.py#L72
49-
# Since we are not using the resulting file, it is safe to use this
50-
# method.
51-
sock_path = tempfile.mktemp(prefix="sock-", dir=tempfile.gettempdir())
52-
return len(sock_path.encode("utf-8")) > MAX_UNIX_SOCKET_PATH
5330

5431
def confirm_tag_in_repo(repo_path: str, tag: str, repo_name: str) -> Optional[str]:
5532
"""Confirm that a given tag exists in a git repository. This function
@@ -823,20 +800,6 @@ def main():
823800
"specify --scheme=foo")
824801
sys.exit(1)
825802

826-
if is_unix_sock_path_too_long():
827-
if not args.dump_hashes and not args.dump_hashes_config:
828-
# Do not print anything other than the json dump.
829-
print(
830-
f"TEMPDIR={tempfile.gettempdir()} is too long and multiprocessing "
831-
"sockets will exceed the size limit. Falling back to verbose mode."
832-
)
833-
args.verbose = True
834-
if sys.version_info.minor < 10:
835-
if not args.dump_hashes and not args.dump_hashes_config:
836-
# Do not print anything other than the json dump.
837-
print("Falling back to verbose mode due to a Python 3.9 limitation.")
838-
args.verbose = True
839-
840803
clone = args.clone
841804
clone_with_ssh = args.clone_with_ssh
842805
skip_history = args.skip_history

0 commit comments

Comments
 (0)