5 from pybind11_tests
import gil_scoped
as m
8 def _run_in_process(target, *args, **kwargs):
9 """Runs target in process and returns its exitcode after 10s (None if still alive)."""
10 process = multiprocessing.Process(target=target, args=args, kwargs=kwargs)
15 process.join(timeout=10)
16 return process.exitcode
18 if process.is_alive():
22 def _python_to_cpp_to_python():
23 """Calls different C++ functions that come back to Python."""
25 class ExtendedVirtClass(m.VirtClass):
26 def virtual_func(self):
29 def pure_virtual_func(self):
32 extended = ExtendedVirtClass()
33 m.test_callback_py_obj(
lambda:
None)
34 m.test_callback_std_func(
lambda:
None)
35 m.test_callback_virtual_func(extended)
36 m.test_callback_pure_virtual_func(extended)
39 def _python_to_cpp_to_python_from_threads(num_threads, parallel=False):
40 """Calls different C++ functions that come back to Python, from Python threads."""
42 for _
in range(num_threads):
43 thread = threading.Thread(target=_python_to_cpp_to_python)
47 threads.append(thread)
50 for thread
in threads:
56 """Makes sure there is no GIL deadlock when running in a thread.
58 It runs in a separate process to be able to stop and assert if it deadlocks.
60 assert _run_in_process(_python_to_cpp_to_python_from_threads, 1) == 0
65 """Makes sure there is no GIL deadlock when running in a thread multiple times in parallel.
67 It runs in a separate process to be able to stop and assert if it deadlocks.
69 assert _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=
True) == 0
74 """Makes sure there is no GIL deadlock when running in a thread multiple times sequentially.
76 It runs in a separate process to be able to stop and assert if it deadlocks.
79 _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=
False) == 0
85 """Makes sure there is no GIL deadlock when using processes.
87 This test is for completion, but it was never an issue.
89 assert _run_in_process(_python_to_cpp_to_python) == 0
93 """Makes sure that the GIL can be acquired by another module from a GIL-released state."""
94 m.test_cross_module_gil()