[3.13] gh-126631: gh-137996: fix pre-loading of `__main__` (GH-135295… · python/cpython@9a6137a · GitHub
Skip to content

Commit 9a6137a

Browse files
gpsheadduaneg
andauthored
[3.13] gh-126631: gh-137996: fix pre-loading of __main__ (GH-135295) (#138609)
gh-126631: gh-137996: fix pre-loading of `__main__` The `main_path` parameter was renamed `init_main_from_name`, update the forkserver code accordingly. This was leading to slower startup times when people were trying to preload the main module. --------- (cherry picked from commit 0912b3a) Co-authored-by: Duane Griffin <duaneg@dghda.com>
1 parent bb17140 commit 9a6137a

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

Lib/multiprocessing/forkserver.py

Lines changed: 6 additions & 5 deletions

Lib/test/_test_multiprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,6 +6545,18 @@ def child():
65456545
self.assertEqual(q.get_nowait(), "done")
65466546
close_queue(q)
65476547

6548+
def test_preload_main(self):
6549+
# gh-126631: Check that __main__ can be pre-loaded
6550+
if multiprocessing.get_start_method() != "forkserver":
6551+
self.skipTest("forkserver specific test")
6552+
6553+
name = os.path.join(os.path.dirname(__file__), 'mp_preload_main.py')
6554+
_, out, err = test.support.script_helper.assert_python_ok(name)
6555+
self.assertEqual(err, b'')
6556+
6557+
# The trailing empty string comes from split() on output ending with \n
6558+
out = out.decode().split("\n")
6559+
self.assertEqual(out, ['__main__', '__mp_main__', 'f', 'f', ''])
65486560

65496561
#
65506562
# Mixins

Lib/test/mp_preload_main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import multiprocessing
2+
3+
print(f"{__name__}")
4+
5+
def f():
6+
print("f")
7+
8+
if __name__ == "__main__":
9+
ctx = multiprocessing.get_context("forkserver")
10+
ctx.set_forkserver_preload(['__main__'])
11+
for _ in range(2):
12+
p = ctx.Process(target=f)
13+
p.start()
14+
p.join()
Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)