Python kill thread

I am trying to kill a thread in python. An exception would be the preferred way to do this, as the elegant output of the run thread method through try: except: pair will allow you to close resources.

I tried: Is there a way to kill Thread in Python? , but indicates that this does not work while the code makes a system call (like time.sleep). Is there a way to raise an exception in another thread (or a process, don't mind) that doesn't work, which thread is executing?

+5
source share
1 answer

In general, raising asynchronous exceptions is difficult to handle properly. This is because instead of having single specific code codes where an exception can be thrown, and therefore when exception handling should be tested, an exception can be thrown after any bytecode instruction instead. This greatly complicates the implementation and fully checks for exception handling.

However, this can be done, but is currently not safe in Python. Increasing the asynchrony of an exception in Python is dangerous because you can raise it during the exception handler, which will lead to another exception and prevent incorrect cleaning. See My answer on How to limit the execution time of a function call in Python .

, Python. , .

+4

All Articles