test_gil_scoped.py
Go to the documentation of this file.
1 import multiprocessing
2 import threading
3 
4 from pybind11_tests import gil_scoped as m
5 
6 
7 def _run_in_process(target, *args, **kwargs):
8  """Runs target in process and returns its exitcode after 10s (None if still alive)."""
9  process = multiprocessing.Process(target=target, args=args, kwargs=kwargs)
10  process.daemon = True
11  try:
12  process.start()
13  # Do not need to wait much, 10s should be more than enough.
14  process.join(timeout=10)
15  return process.exitcode
16  finally:
17  if process.is_alive():
18  process.terminate()
19 
20 
22  """Calls different C++ functions that come back to Python."""
23 
24  class ExtendedVirtClass(m.VirtClass):
25  def virtual_func(self):
26  pass
27 
28  def pure_virtual_func(self):
29  pass
30 
31  extended = ExtendedVirtClass()
32  m.test_callback_py_obj(lambda: None)
33  m.test_callback_std_func(lambda: None)
34  m.test_callback_virtual_func(extended)
35  m.test_callback_pure_virtual_func(extended)
36 
37 
38 def _python_to_cpp_to_python_from_threads(num_threads, parallel=False):
39  """Calls different C++ functions that come back to Python, from Python threads."""
40  threads = []
41  for _ in range(num_threads):
42  thread = threading.Thread(target=_python_to_cpp_to_python)
43  thread.daemon = True
44  thread.start()
45  if parallel:
46  threads.append(thread)
47  else:
48  thread.join()
49  for thread in threads:
50  thread.join()
51 
52 
53 # TODO: FIXME, sometimes returns -11 (segfault) instead of 0 on macOS Python 3.9
55  """Makes sure there is no GIL deadlock when running in a thread.
56 
57  It runs in a separate process to be able to stop and assert if it deadlocks.
58  """
59  assert _run_in_process(_python_to_cpp_to_python_from_threads, 1) == 0
60 
61 
62 # TODO: FIXME on macOS Python 3.9
64  """Makes sure there is no GIL deadlock when running in a thread multiple times in parallel.
65 
66  It runs in a separate process to be able to stop and assert if it deadlocks.
67  """
68  assert _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=True) == 0
69 
70 
71 # TODO: FIXME on macOS Python 3.9
73  """Makes sure there is no GIL deadlock when running in a thread multiple times sequentially.
74 
75  It runs in a separate process to be able to stop and assert if it deadlocks.
76  """
77  assert (
78  _run_in_process(_python_to_cpp_to_python_from_threads, 8, parallel=False) == 0
79  )
80 
81 
82 # TODO: FIXME on macOS Python 3.9
84  """Makes sure there is no GIL deadlock when using processes.
85 
86  This test is for completion, but it was never an issue.
87  """
88  assert _run_in_process(_python_to_cpp_to_python) == 0
89 
90 
92  """Makes sure that the GIL can be acquired by another module from a GIL-released state."""
93  m.test_cross_module_gil() # Should not raise a SIGSEGV
def _run_in_process(target, args, kwargs)
def test_python_to_cpp_to_python_from_thread_multiple_parallel()
def _python_to_cpp_to_python()
def test_python_to_cpp_to_python_from_process()
def _python_to_cpp_to_python_from_threads(num_threads, parallel=False)
Double_ range(const Point2_ &p, const Point2_ &q)
def test_cross_module_gil()
def test_python_to_cpp_to_python_from_thread_multiple_sequential()
def test_python_to_cpp_to_python_from_thread()


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45