summaryrefslogtreecommitdiff
path: root/paste/util/killthread.py
diff options
context:
space:
mode:
authorMarc Abramowitz <marc@marc-abramowitz.com>2015-04-30 16:42:17 -0700
committerMarc Abramowitz <marc@marc-abramowitz.com>2015-04-30 16:42:17 -0700
commit12a3f1f4cfa7f88478dc1b0e949fcc095b9fc804 (patch)
treeddb8079523d846f0b074437fc33fa5e28b508183 /paste/util/killthread.py
downloadpaste-git-eliminate_cgi_parse_qsl_2.tar.gz
Replace cgi.parse_qsl w/ six.moves.urllib.parse.parse_sqleliminate_cgi_parse_qsl_2eliminate_cgi_parse_qsl
because `cgi.parse_qsl` is deprecated, according to https://docs.python.org/2/library/cgi.html#cgi.parse_qsl
Diffstat (limited to 'paste/util/killthread.py')
-rw-r--r--paste/util/killthread.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/paste/util/killthread.py b/paste/util/killthread.py
new file mode 100644
index 0000000..4df4f42
--- /dev/null
+++ b/paste/util/killthread.py
@@ -0,0 +1,30 @@
+"""
+Kill a thread, from http://sebulba.wikispaces.com/recipe+thread2
+"""
+import six
+try:
+ import ctypes
+except ImportError:
+ raise ImportError(
+ "You cannot use paste.util.killthread without ctypes installed")
+if not hasattr(ctypes, 'pythonapi'):
+ raise ImportError(
+ "You cannot use paste.util.killthread without ctypes.pythonapi")
+
+def async_raise(tid, exctype):
+ """raises the exception, performs cleanup if needed.
+
+ tid is the value given by thread.get_ident() (an integer).
+ Raise SystemExit to kill a thread."""
+ if not isinstance(exctype, (six.class_types, type)):
+ raise TypeError("Only types can be raised (not instances)")
+ if not isinstance(tid, int):
+ raise TypeError("tid must be an integer")
+ res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
+ if res == 0:
+ raise ValueError("invalid thread id")
+ elif res != 1:
+ # """if it returns a number greater than one, you're in trouble,
+ # and you should call it again with exc=NULL to revert the effect"""
+ ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), 0)
+ raise SystemError("PyThreadState_SetAsyncExc failed")