根据 thread id 停止一个线程
出自 https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/middleware/threading.py#L62
thread_id = threading.get_ident()
1 def _raise_thread_exception_cpython(thread_id, exception): 2 exctype = (exception if inspect.isclass(exception) else type(exception)).__name__ 3 thread_id = ctypes.c_long(thread_id) 4 exception = ctypes.py_object(exception) 5 count = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, exception) 6 if count == 0: 7 logger.critical("Failed to set exception (%s) in thread %r.", exctype, thread_id.value) 8 elif count > 1: # pragma: no cover 9 logger.critical("Exception (%s) was set in multiple threads. Undoing...", exctype) 10 ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, ctypes.c_long(0))
https://docs.python.org/3/c-api/init.html#c.PyThreadState_SetAsyncExc
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步