Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I kill a thread in python [duplicate]

I start a thread using the following code.

t = thread.start_new_thread(myfunction)

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

t.kill()

Note that I'm using Python 2.4.

like image 241
pythonic Avatar asked Jul 11 '12 11:07

pythonic


People also ask

How do I force a thread to kill?

Back in the main thread of our first process, we will block for a while to let the new task run. We can then forcefully kill the new main thread by calling the terminate() function on the thread's parent process. This will stop the thread immediately.

How do I stop a thread loop?

Basically you just need to set up the thread with a stop function that sets a sentinel value that the thread will check. In your case, you'll have the something in your loop check the sentinel value to see if it's changed and if it has, the loop can break and the thread can die.

How do you stop a daemon thread in Python?

This process of stopping the daemon thread can be automated using the atexit module. The atexit module allows a function to be registered that will be called by the Python interpreter right before the process is exited.


3 Answers

In Python, you simply cannot kill a Thread.

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

yourProcess.terminate()  # kill the process!

Python will kill your process (on Unix through the SIGTERM signal, while on Windows through the TerminateProcess() call). Pay attention to use it while using a Queue or a Pipe! (it may corrupt the data in the Queue/Pipe)

Note that the multiprocessing.Event and the multiprocessing.Semaphore work exactly in the same way of the threading.Event and the threading.Semaphore respectively. In fact, the first ones are clones of the latters.

If you REALLY need to use a Thread, there is no way to kill your threads directly. What you can do, however, is to use a "daemon thread". In fact, in Python, a Thread can be flagged as daemon:

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

The main program will exit when no alive non-daemon threads are left. In other words, when your main thread (which is, of course, a non-daemon thread) will finish its operations, the program will exit even if there are still some daemon threads working.

Note that it is necessary to set a Thread as daemon before the start() method is called!

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

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

like image 70
Paolo Rovelli Avatar answered Oct 26 '22 14:10

Paolo Rovelli


If your thread is busy executing Python code, you have a bigger problem than the inability to kill it. The GIL will prevent any other thread from even running whatever instructions you would use to do the killing. (After a bit of research, I've learned that the interpreter periodically releases the GIL, so the preceding statement is bogus. The remaining comment stands, however.)

Your thread must be written in a cooperative manner. That is, it must periodically check in with a signalling object such as a semaphore, which the main thread can use to instruct the worker thread to voluntarily 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()
like image 35
Marcelo Cantos Avatar answered Oct 26 '22 14:10

Marcelo Cantos


You can't kill a thread from another thread. You need to signal to the other thread that it should end. And by "signal" I don't mean use the signal function, I mean that you have to arrange for some communication between the threads.

like image 25
Ned Batchelder Avatar answered Oct 26 '22 15:10

Ned Batchelder