Skip to content

Commit 9101b39

Browse files
authored
To fix the random failed test cases of test___xxsubinterpreters in multiprocess. (GH-27240)
1 parent ccefa8a commit 9101b39

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

‎Lib/test/test__xxsubinterpreters.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ def _run_output(interp, request, shared=None):
3939
return rpipe.read()
4040

4141

42+
def _wait_for_interp_to_run(interp, timeout=None):
43+
# bpo-37224: Running this test file in multiprocesses will fail randomly.
44+
# The failure reason is that the thread can't acquire the cpu to
45+
# run subinterpreter eariler than the main thread in multiprocess.
46+
if timeout is None:
47+
timeout = support.SHORT_TIMEOUT
48+
start_time = time.monotonic()
49+
deadline = start_time + timeout
50+
while not interpreters.is_running(interp):
51+
if time.monotonic() > deadline:
52+
raise RuntimeError('interp is not running')
53+
time.sleep(0.010)
54+
55+
4256
@contextlib.contextmanager
4357
def _running(interp):
4458
r, w = os.pipe()
@@ -51,6 +65,7 @@ def run():
5165

5266
t = threading.Thread(target=run)
5367
t.start()
68+
_wait_for_interp_to_run(interp)
5469

5570
yield
5671

0 commit comments

Comments
 (0)