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