Skip to content
Snippets Groups Projects
Commit 0135fa62 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add option to force thread to exit

parent c67930e3
No related branches found
No related tags found
No related merge requests found
import threading
import ctypes
import threading
class TerminableThread(threading.Thread):
......@@ -15,6 +15,7 @@ class TerminableThread(threading.Thread):
Flag whether to terminate is stored in self._terminating
"""
def __init__(self):
super().__init__()
self._terminating = False
......@@ -49,18 +50,8 @@ class TerminableThread(threading.Thread):
"""
self._terminating = True
if force:
found = False
for tid, tobj in threading._active.items():
if tobj is self:
found = True
target_tid = tid
break
if not found:
raise ValueError('Thread ID for thread %s was not found' % (self, ))
ret = ctypes.pythonapi.PyThreadState_SetAsyncExc(target_tid, ctypes.py_object(SystemExit))
ret = ctypes.pythonapi.PyThreadState_SetAsyncExc(self._ident, ctypes.py_object(SystemExit))
if ret == 0:
raise ValueError('Invalid thread ID %s obtained for %s' % (target_tid, self))
raise ValueError('Invalid thread ID %s obtained for %s' % (self._ident, self))
return self
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment