8000 gh-105716: Support Background Threads in Subinterpreters Consistently by ericsnowcurrently · Pull Request #109921 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105716: Support Background Threads in Subinterpreters Consistently #109921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4e88c2b
Always wait for threads in subinterpreters.
ericsnowcurrently Sep 25, 2023
d08ac68
Add PyInterpreterState_*RunningMain().
ericsnowcurrently Sep 26, 2023
3b07482
Drop interpreter_exists().
ericsnowcurrently Sep 26, 2023
70c4753
Drop PyThreadState_IsRunning().
ericsnowcurrently Sep 26, 2023
678cb3b
Add a TODO.
ericsnowcurrently Sep 26, 2023
5be5a07
For now use the earliest thread state.
ericsnowcurrently Sep 26, 2023
81a1e0b
Add tests.
ericsnowcurrently Sep 26, 2023
c3f497b
Mark the main interpreter as running __main__.
ericsnowcurrently Sep 26, 2023
effa5b4
Call PyInterpreterState_SetNotRunningMain() in the right place.
ericsnowcurrently Sep 26, 2023
41c54b0
Check PyInterpreterState_IsRunningMain() preemptively, for now.
ericsnowcurrently Sep 26, 2023
5fbd940
Add docs.
ericsnowcurrently Sep 26, 2023
8b54c2e
Add a NEWS entry.
ericsnowcurrently Sep 26, 2023
5d1df61
Fix a typo.
ericsnowcurrently Sep 27, 2023
1034a67
Update the docs.
ericsnowcurrently Sep 27, 2023
d57729e
Update the docs.
ericsnowcurrently Sep 27, 2023
4219e2c
Update the docs.
ericsnowcurrently Sep 27, 2023
bfdace2
Drop a dead line.
ericsnowcurrently Sep 27, 2023
ef8af92
Drop an unused parameter.
ericsnowcurrently Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests.
  • Loading branch information
ericsnowcurrently committed Sep 26, 2023
commit 81a1e0b3de8916455a97a0e39ae09c2893af463f
97 changes: 97 additions & 0 deletions Lib/test/test_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ def test_subinterpreter(self):
self.assertTrue(interp.is_running())
self.assertFalse(interp.is_running())

def test_finished(self):
r, w = os.pipe()
interp = interpreters.create()
interp.run(f"""if True:
import os
os.write({w}, b'x')
""")
self.assertFalse(interp.is_running())
self.assertEqual(os.read(r, 1), b'x')

def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent(f"""
Expand Down Expand Up @@ -284,6 +294,31 @@ def test_bad_id(self):
with self.assertRaises(ValueError):
interp.is_running()

def test_with_only_background_threads(self):
r_interp, w_interp = os.pipe()
r_thread, w_thread = os.pipe()

DONE = b'D'
FINISHED = b'F'

interp = interpreters.create()
interp.run(f"""if True:
import os
import threading

def task():
v = os.read({r_thread}, 1)
assert v == {DONE!r}
os.write({w_interp}, {FINISHED!r})
t = threading.Thread(target=task)
t.start()
""")
self.assertFalse(interp.is_running())

os.write(w_thread, DONE)
interp.run('t.join()')
self.assertEqual(os.read(r_interp, 1), FINISHED)


class TestInterpreterClose(TestBase):

Expand Down Expand Up @@ -385,6 +420,37 @@ def test_still_running(self):
interp.close()
self.assertTrue(interp.is_running())

def test_subthreads_still_running(self):
r_interp, w_interp = os.pipe()
r_thread, w_thread = os.pipe()

FINISHED = b'F'

interp = interpreters.create()
interp.run(f"""if True:
import os
import threading
import time

done = False

def notify_fini():
global done
done = True
t.join()
threading._register_atexit(notify_fini)

def task():
while not done:
time.sleep(0.1)
os.write({w_interp}, {FINISHED!r})
t = threading.Thread(target=task)
t.start()
""")
interp.close()

self.assertEqual(os.read(r_interp, 1), FINISHED)


class TestInterpreterRun(TestBase):

Expand Down Expand Up @@ -461,6 +527,37 @@ def test_bytes_for_script(self):
with self.assertRaises(TypeError):
interp.run(b'print("spam")')

def test_with_background_threads_still_running(self):
r_interp, w_interp = os.pipe()
r_thread, w_thread = os.pipe()

RAN = b'R'
DONE = b'D'
FINISHED = b'F'

interp = interpreters.create()
interp.run(f"""if True:
import os
import threading

def task():
v = os.read({r_thread}, 1)
assert v == {DONE!r}
os.write({w_interp}, {FINISHED!r})
t = threading.Thread(target=task)
t.start()
os.write({w_interp}, {RAN!r})
""")
interp.run(f"""if True:
os.write({w_interp}, {RAN!r})
""")

os.write(w_thread, DONE)
interp.run('t.join()')
self.assertEqual(os.read(r_interp, 1), RAN)
self.assertEqual(os.read(r_interp, 1), RAN)
self.assertEqual(os.read(r_interp, 1), FINISHED)

# test_xxsubinterpreters covers the remaining Interpreter.run() behavior.


Expand Down
0