As the title suggests, how do I kill a python thread from the main thread that created it? I am currently running 3rd party code in a separate thread (say thread 2) from my main thread and sometimes their code simply hits an infinite loop that is bounded with a blanket try/except statement. This means that I cannot simply raise an exception in thread 2 to stop it. Also, I cannot modify the 3rd party code to make their code more thread friendly. I understand based on the many questions from stack overflow that killing a thread is usually a bad pattern to use, but I believe this is the best solution in the current circumstance.
Asked
Active
Viewed 126 times
0
-
It's not just a bad pattern, it's also impossible. – wRAR Feb 28 '13 at 00:56
-
1There are tricks that you can do in different cases (e.g., you can always `ctypes` down to your platform's TerminateThread/thread_cancel/whatever), and workarounds that makes sense for different uses (e.g., processes instead of threads). But all of those tricks, and their downsides, are already discussed on the question I linked above, so there's no reason to go through them again. – abarnert Feb 28 '13 at 00:57
-
1You could perhaps run the 3rd party code in a separate process (using the `multiprocessing` module) and call `terminate` on the process. – unutbu Feb 28 '13 at 00:58