|
20 | 20 | from multiprocessing import Pool |
21 | 21 | from invoke import task |
22 | 22 | from invoke.context import Context |
| 23 | +from contextlib import contextmanager |
| 24 | + |
| 25 | + |
| 26 | +@contextmanager |
| 27 | +def managed_pool(pool_size): |
| 28 | + """ |
| 29 | + Context manager for multiprocessing Pool that ensures proper cleanup. |
| 30 | + |
| 31 | + Automatically handles pool.close() and pool.join() when exiting the context. |
| 32 | + """ |
| 33 | + pool = Pool(pool_size) |
| 34 | + try: |
| 35 | + yield pool |
| 36 | + finally: |
| 37 | + pool.close() |
| 38 | + pool.join() |
23 | 39 |
|
24 | 40 |
|
25 | 41 | def fetch_github_releases(): |
@@ -348,19 +364,14 @@ def pull(c, version, cpu=None): |
348 | 364 |
|
349 | 365 | print(f" -- Found {len(versions)} version(s) to pull") |
350 | 366 |
|
351 | | - pool = Pool(get_pool_size(cpu)) |
352 | | - |
353 | 367 | configs = [ |
354 | 368 | {'context': c, 'version': v} |
355 | 369 | for v in versions |
356 | 370 | ] |
357 | 371 |
|
358 | | - try: |
| 372 | + with managed_pool(get_pool_size(cpu)) as pool: |
359 | 373 | pool.map(_docker_pull, configs) |
360 | 374 | print(f" -- Completed pulling {len(versions)} version(s)") |
361 | | - finally: |
362 | | - pool.close() |
363 | | - pool.join() |
364 | 375 |
|
365 | 376 |
|
366 | 377 | @task(help={ |
@@ -401,19 +412,14 @@ def build(c, version, cpu=None): |
401 | 412 |
|
402 | 413 | print(f" -- Found {len(versions)} version(s) to build") |
403 | 414 |
|
404 | | - pool = Pool(get_pool_size(cpu)) |
405 | | - |
406 | 415 | configs = [ |
407 | 416 | {'context': c, 'version': v} |
408 | 417 | for v in versions |
409 | 418 | ] |
410 | 419 |
|
411 | | - try: |
| 420 | + with managed_pool(get_pool_size(cpu)) as pool: |
412 | 421 | pool.map(_docker_build, configs) |
413 | 422 | print(f" -- Completed building {len(versions)} version(s)") |
414 | | - finally: |
415 | | - pool.close() |
416 | | - pool.join() |
417 | 423 |
|
418 | 424 |
|
419 | 425 | @task(help={ |
@@ -454,19 +460,14 @@ def push(c, version, cpu=None): |
454 | 460 |
|
455 | 461 | print(f" -- Found {len(versions)} version(s) to push") |
456 | 462 |
|
457 | | - pool = Pool(get_pool_size(cpu)) |
458 | | - |
459 | 463 | configs = [ |
460 | 464 | {'context': c, 'version': v} |
461 | 465 | for v in versions |
462 | 466 | ] |
463 | 467 |
|
464 | | - try: |
| 468 | + with managed_pool(get_pool_size(cpu)) as pool: |
465 | 469 | pool.map(_docker_push, configs) |
466 | 470 | print(f" -- Completed pushing {len(versions)} version(s)") |
467 | | - finally: |
468 | | - pool.close() |
469 | | - pool.join() |
470 | 471 |
|
471 | 472 |
|
472 | 473 | @task(help={}) |
|
0 commit comments