1 """Threading util helpers."""
9 THREADING_SHUTDOWN_TIMEOUT = 10
11 _LOGGER = logging.getLogger(__name__)
15 """Shutdown that will not deadlock."""
21 for thread
in threading.enumerate()
22 if thread
is not threading.main_thread()
27 if not remaining_threads:
30 timeout_per_thread = THREADING_SHUTDOWN_TIMEOUT / len(remaining_threads)
31 for thread
in remaining_threads:
33 thread.join(timeout_per_thread)
34 except Exception
as err:
35 _LOGGER.warning(
"Failed to join thread: %s", err)
39 """Raise an exception in the threads with id tid."""
40 if not inspect.isclass(exctype):
41 raise TypeError(
"Only types can be raised (not instances)")
43 c_tid = ctypes.c_ulong(tid)
44 res = ctypes.pythonapi.PyThreadState_SetAsyncExc(c_tid, ctypes.py_object(exctype))
51 ctypes.pythonapi.PyThreadState_SetAsyncExc(c_tid,
None)
52 raise SystemError(
"PyThreadState_SetAsyncExc failed")
56 """A thread class that supports raising exception in the thread from another thread.
59 https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread/49877671
64 """Raise the given exception type in the context of this thread."""
None raise_exc(self, Any exctype)
None deadlock_safe_shutdown()
None async_raise(int tid, Any exctype)