diff options
author | Mike Frysinger <vapier@google.com> | 2021-02-16 01:56:30 -0500 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-02-22 22:51:46 +0000 |
commit | 15e807cf3c5d3bf7e142f74edea219514caa749a (patch) | |
tree | 58d489bd23c7abcaf99ebd86e0b70c95b775e14e /subcmds/forall.py | |
parent | 7c871163c8803e812998e5b2296e3bbb30b1367f (diff) | |
download | git-repo-15e807cf3c5d3bf7e142f74edea219514caa749a.tar.gz |
forall: improve pool logic
Use a pool contextmanager to take care of the messy details like
properly cleaning it up when aborting.
Change-Id: I264ebb591c2e67c9a975b6dcc0f14b29cc66a874
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/297243
Reviewed-by: Chris Mcdonald <cjmcdonald@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'subcmds/forall.py')
-rw-r--r-- | subcmds/forall.py | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/subcmds/forall.py b/subcmds/forall.py index d871b3ea..b874b6d2 100644 --- a/subcmds/forall.py +++ b/subcmds/forall.py | |||
@@ -21,7 +21,7 @@ import sys | |||
21 | import subprocess | 21 | import subprocess |
22 | 22 | ||
23 | from color import Coloring | 23 | from color import Coloring |
24 | from command import DEFAULT_LOCAL_JOBS, Command, MirrorSafeCommand | 24 | from command import DEFAULT_LOCAL_JOBS, Command, MirrorSafeCommand, WORKER_BATCH_SIZE |
25 | import platform_utils | 25 | import platform_utils |
26 | 26 | ||
27 | _CAN_COLOR = [ | 27 | _CAN_COLOR = [ |
@@ -234,31 +234,26 @@ without iterating through the remaining projects. | |||
234 | 234 | ||
235 | os.environ['REPO_COUNT'] = str(len(projects)) | 235 | os.environ['REPO_COUNT'] = str(len(projects)) |
236 | 236 | ||
237 | pool = multiprocessing.Pool(opt.jobs, InitWorker) | ||
238 | try: | 237 | try: |
239 | config = self.manifest.manifestProject.config | 238 | config = self.manifest.manifestProject.config |
240 | results_it = pool.imap( | 239 | with multiprocessing.Pool(opt.jobs, InitWorker) as pool: |
241 | DoWorkWrapper, | 240 | results_it = pool.imap( |
242 | self.ProjectArgs(projects, mirror, opt, cmd, shell, config)) | 241 | DoWorkWrapper, |
243 | pool.close() | 242 | self.ProjectArgs(projects, mirror, opt, cmd, shell, config), |
244 | for r in results_it: | 243 | chunksize=WORKER_BATCH_SIZE) |
245 | rc = rc or r | 244 | for r in results_it: |
246 | if r != 0 and opt.abort_on_errors: | 245 | rc = rc or r |
247 | raise Exception('Aborting due to previous error') | 246 | if r != 0 and opt.abort_on_errors: |
247 | raise Exception('Aborting due to previous error') | ||
248 | except (KeyboardInterrupt, WorkerKeyboardInterrupt): | 248 | except (KeyboardInterrupt, WorkerKeyboardInterrupt): |
249 | # Catch KeyboardInterrupt raised inside and outside of workers | 249 | # Catch KeyboardInterrupt raised inside and outside of workers |
250 | print('Interrupted - terminating the pool') | ||
251 | pool.terminate() | ||
252 | rc = rc or errno.EINTR | 250 | rc = rc or errno.EINTR |
253 | except Exception as e: | 251 | except Exception as e: |
254 | # Catch any other exceptions raised | 252 | # Catch any other exceptions raised |
255 | print('Got an error, terminating the pool: %s: %s' % | 253 | print('Got an error, terminating the pool: %s: %s' % |
256 | (type(e).__name__, e), | 254 | (type(e).__name__, e), |
257 | file=sys.stderr) | 255 | file=sys.stderr) |
258 | pool.terminate() | ||
259 | rc = rc or getattr(e, 'errno', 1) | 256 | rc = rc or getattr(e, 'errno', 1) |
260 | finally: | ||
261 | pool.join() | ||
262 | if rc != 0: | 257 | if rc != 0: |
263 | sys.exit(rc) | 258 | sys.exit(rc) |
264 | 259 | ||