summaryrefslogtreecommitdiff
path: root/paste/util
diff options
context:
space:
mode:
authorianb <devnull@localhost>2007-01-30 05:11:25 +0000
committerianb <devnull@localhost>2007-01-30 05:11:25 +0000
commitf3a41d80959005d2ad273aa5c88bad1957bb992e (patch)
tree1ff5206683898a7dbdc2a7c6309d77cc9d1496e7 /paste/util
parentff149b45ee82a904541fe8208414f8fd56b7ecaa (diff)
downloadpaste-f3a41d80959005d2ad273aa5c88bad1957bb992e.tar.gz
Allow killing of errant threads through the web application (also add a module to kill threads with ctypes, and a method to kill threads from the httpserver thread pool)
Diffstat (limited to 'paste/util')
-rw-r--r--paste/util/killthread.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/paste/util/killthread.py b/paste/util/killthread.py
new file mode 100644
index 0000000..cf9e2b7
--- /dev/null
+++ b/paste/util/killthread.py
@@ -0,0 +1,27 @@
+"""
+Kill a thread, from http://sebulba.wikispaces.com/recipe+thread2
+"""
+import types
+try:
+ import ctypes
+except ImportError:
+ raise ImportError(
+ "You cannot use paste.util.killthread without ctypes installed")
+
+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, (types.ClassType, 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(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(tid, 0)
+ raise SystemError("PyThreadState_SetAsyncExc failed")