Unverified Kaydet (Commit) 6cdce3dd authored tarafından Victor Stinner's avatar Victor Stinner Kaydeden (comit) GitHub

bpo-35424: Fix test_multiprocessing_main_handling (GH-11223)

Fix test_multiprocessing_main_handling: use multiprocessing.Pool with
a context manager and then explicitly join the pool.
üst 05c9d31e
...@@ -54,18 +54,21 @@ if "check_sibling" in __file__: ...@@ -54,18 +54,21 @@ if "check_sibling" in __file__:
if __name__ == '__main__': if __name__ == '__main__':
start_method = sys.argv[1] start_method = sys.argv[1]
set_start_method(start_method) set_start_method(start_method)
p = Pool(5)
results = [] results = []
p.map_async(f, [1, 2, 3], callback=results.extend) with Pool(5) as pool:
start_time = time.monotonic() pool.map_async(f, [1, 2, 3], callback=results.extend)
while not results: start_time = time.monotonic()
time.sleep(0.05) while not results:
# up to 1 min to report the results time.sleep(0.05)
dt = time.monotonic() - start_time # up to 1 min to report the results
if dt > 60.0: dt = time.monotonic() - start_time
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt) if dt > 60.0:
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
results.sort() results.sort()
print(start_method, "->", results) print(start_method, "->", results)
pool.join()
""" """
test_source_main_skipped_in_children = """\ test_source_main_skipped_in_children = """\
...@@ -84,18 +87,21 @@ from multiprocessing import Pool, set_start_method ...@@ -84,18 +87,21 @@ from multiprocessing import Pool, set_start_method
start_method = sys.argv[1] start_method = sys.argv[1]
set_start_method(start_method) set_start_method(start_method)
p = Pool(5)
results = [] results = []
p.map_async(int, [1, 4, 9], callback=results.extend) with Pool(5) as pool:
start_time = time.monotonic() pool.map_async(int, [1, 4, 9], callback=results.extend)
while not results: start_time = time.monotonic()
time.sleep(0.05) while not results:
# up to 1 min to report the results time.sleep(0.05)
dt = time.monotonic() - start_time # up to 1 min to report the results
if dt > 60.0: dt = time.monotonic() - start_time
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt) if dt > 60.0:
raise RuntimeError("Timed out waiting for results (%.1f sec)" % dt)
results.sort() results.sort()
print(start_method, "->", results) print(start_method, "->", results)
pool.join()
""" """
# These helpers were copied from test_cmd_line_script & tweaked a bit... # These helpers were copied from test_cmd_line_script & tweaked a bit...
......
Fix test_multiprocessing_main_handling: use :class:`multiprocessing.Pool` with
a context manager and then explicitly join the pool.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment