How can I kill a thread in python

I start the stream using the following code.

t = thread.start_new_thread(myfunction) 

How can I kill thread t from another thread. So basically, in terms of code, I want to be able to do something like this.

 t.kill() 

Please note that I am using Python 2.4.

+6
source share
3 answers

If your thread is busy executing Python code, you have more trouble than not being able to kill it. GIL will prevent any other thread from even following any instructions that you would use to kill. (After a little research, I found out that the interpreter periodically releases GIL, so the previous statement is fictitious. The rest of the comments are, however.)

Your thread must be written together. That is, it must periodically register with a signal object, such as a semaphore, which the main thread can use to instruct the workflow for voluntary exit.

 while not sema.acquire(False): # Do a small portion of work… 

or

 for item in work: # Keep working… # Somewhere deep in the bowels… if sema.acquire(False): thread.exit() 
+10
source

In Python, you simply cannot kill Thread.

If you really don't need to have Thread (!), What you can do, instead of using a streaming package ( http://docs.python.org/2/library/threading.html ), is to use a multiprocessing package ( http: / /docs.python.org/2/library/multiprocessing.html ). To kill a process, you can simply call the method:

 yourProcess.terminate() # kill the process! 

Python will kill your process (on Unix through a SIGTERM signal, while on Windows through a call to TerminateProcess ()). Pay attention to use it when using a queue or pipe! (this may damage the data in the queue / handset)

Note that the .Event multiprocessing process is also multiprocessing. Semaphores work just like threading.Event and threading.Semaphore respectively. In fact, the former are clones of the latter.

If you really need to use Thread, it is not possible to directly kill your threads. However, you can use the "daemon stream". In fact, in Python, Thread can be marked as a daemon:

 yourThread.daemon = True # set the Thread as a "daemon thread" 

The main program will exit if there is not a single living non-demon left. In other words, when your main thread (which, of course, a non-daemon thread) finishes its work, the program will exit, even if there are still some daemon threads.

Note that before calling the start () method, you must set Thread as a daemon!

Of course, you can and should use the daemon even with multiprocessing. Here, when the main process ends, it tries to terminate all of its demonic child processes.

Finally, note that sys.exit () and os.kill () are not choices.

+14
source

You cannot kill a thread from another thread. You need to send a signal to another thread, which it must complete. And by "signal" I do not mean using the signal function, I mean that you need to organize some communication between the threads.

+7
source

Source: https://habr.com/ru/post/920213/


All Articles