diff options
author | matt <matt@xcolour.net> | 2013-01-28 11:32:18 -0500 |
---|---|---|
committer | matt <matt@xcolour.net> | 2013-01-28 11:32:18 -0500 |
commit | 1afcb52d73271bbbd78f885451aa1b0e78c09871 (patch) | |
tree | 9145840d6036fcbc0b6647c88f679a567fa8c54d /paste/util/killthread.py | |
download | paste-git-stringio.tar.gz |
Import StringIO so it can be used.stringio
Diffstat (limited to 'paste/util/killthread.py')
-rw-r--r-- | paste/util/killthread.py | 30 |
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..f1fc93f --- /dev/null +++ b/paste/util/killthread.py @@ -0,0 +1,30 @@ +""" +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") +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, (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(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") |