Skip to content

Commit c74e7c4

Browse files
bpo-35424: Fix test_multiprocessing_main_handling (GH-11223) (GH-11227)
Fix test_multiprocessing_main_handling: use multiprocessing.Pool with a context manager and then explicitly join the pool. (cherry picked from commit 6cdce3d) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent 729fc5d commit c74e7c4

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

Lib/test/test_multiprocessing_main_handling.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,21 @@ def f(x):
5454
if __name__ == '__main__':
5555
start_method = sys.argv[1]
5656
set_start_method(start_method)
57-
p = Pool(5)
5857
results = []
59-
p.map_async(f, [1, 2, 3], callback=results.extend)
60-
start_time = time.monotonic()
61-
while not results:
62-
time.sleep(0.05)
63-
# up to 1 min to report the results
64-
dt = time.monotonic() - start_time
65-
if dt > 60.0:
66-
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
58+
with Pool(5) as pool:
59+
pool.map_async(f, [1, 2, 3], callback=results.extend)
60+
start_time = time.monotonic()
61+
while not results:
62+
time.sleep(0.05)
63+
# up to 1 min to report the results
64+
dt = time.monotonic() - start_time
65+
if dt > 60.0:
66+
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
67+
6768
results.sort()
6869
print(start_method, "->", results)
70+
71+
pool.join()
6972
"""
7073

7174
test_source_main_skipped_in_children = """\
@@ -84,18 +87,21 @@ def f(x):
8487
8588
start_method = sys.argv[1]
8689
set_start_method(start_method)
87-
p = Pool(5)
8890
results = []
89-
p.map_async(int, [1, 4, 9], callback=results.extend)
90-
start_time = time.monotonic()
91-
while not results:
92-
time.sleep(0.05)
93-
# up to 1 min to report the results
94-
dt = time.monotonic() - start_time
95-
if dt > 60.0:
96-
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
91+
with Pool(5) as pool:
92+
pool.map_async(int, [1, 4, 9], callback=results.extend)
93+
start_time = time.monotonic()
94+
while not results:
95+
time.sleep(0.05)
96+
# up to 1 min to report the results
97+
dt = time.monotonic() - start_time
98+
if dt > 60.0:
99+
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
100+
97101
results.sort()
98102
print(start_method, "->", results)
103+
104+
pool.join()
99105
"""
100106

101107
# These helpers were copied from test_cmd_line_script & tweaked a bit...
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix test_multiprocessing_main_handling: use :class:`multiprocessing.Pool` with
2+
a context manager and then explicitly join the pool.

0 commit comments

Comments
 (0)