Raise an Exception in the Thread with id `tid`. Perform cleanup if needed. Based on Killable Threads By Tomer Filiba from http://tomerfiliba.com/recipes/Thread2/ license: public domain.
(tid, exctype=Exception)
| 159 | pass |
| 160 | |
| 161 | def async_raise(tid, exctype=Exception): |
| 162 | """ |
| 163 | Raise an Exception in the Thread with id `tid`. Perform cleanup if |
| 164 | needed. |
| 165 | |
| 166 | Based on Killable Threads By Tomer Filiba |
| 167 | from http://tomerfiliba.com/recipes/Thread2/ |
| 168 | license: public domain. |
| 169 | """ |
| 170 | assert isinstance(tid, int), 'Invalid thread id: must an integer' |
| 171 | |
| 172 | tid = c_long(tid) |
| 173 | exception = py_object(Exception) |
| 174 | res = pythonapi.PyThreadState_SetAsyncExc(tid, exception) |
| 175 | if res == 0: |
| 176 | raise ValueError('Invalid thread id.') |
| 177 | elif res != 1: |
| 178 | # if it returns a number greater than one, you're in trouble, |
| 179 | # and you should call it again with exc=NULL to revert the effect |
| 180 | pythonapi.PyThreadState_SetAsyncExc(tid, 0) |
| 181 | raise SystemError('PyThreadState_SetAsyncExc failed.') |
| 182 | |
| 183 | |
| 184 | def fake_interruptible(func, args=None, kwargs=None, timeout=DEFAULT_TIMEOUT): |