1. 程式人生 > >Python強制關閉執行緒的一種辦法(可行已用於專案)

Python強制關閉執行緒的一種辦法(可行已用於專案)

由於經常被Python非Daemon執行緒阻塞,導致程式無法結束。所以到處找辦法解決,但是經常沒有找到點上。導致無功而返。

今天突發奇想來搜了一下相關的解決方案,竟然被我找到了。

首先是百度了一下(懶得開VPN)

然後找到了一個網友分享的解決方案:

http://www.cnblogs.com/rainduck/archive/2013/03/29/2989810.html

但是試驗之後並沒有什麼卵用(┑( ̄Д  ̄)┍),我是在我的MAC上面試驗的。python 2.7.10

然後再次谷歌了一下使用到的API,在最佳回答的評論區找到了答案。

http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python

第六條評論

於是最終解決方案如下:

import threading
import time
import inspect
import ctypes

def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble,
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")

def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)

class TestThread(threading.Thread):
    def run(self):
        print "begin"
        while True:
            time.sleep(0.1)
        print "end"
if __name__ == "__main__":
    t = TestThread()
    t.start()
    time.sleep(1)
    stop_thread(t)
    print "stoped"


附上我的程式碼:

    ##將每次訓練任務放到一個獨立的執行緒中進行,實現多執行緒
    def startTrain(self):
        refreshParam()
        self.__threadTrain=threading.Thread(target=self.trainmodel)
        self.train_flag = False
        self.__threadTrain.setDaemon(True)
        self.__threadTrain.start()
#        self.currentthread = self.__threadTrain.getName()
        if not self.train_flag :
            self.periodicTextCall()
        else:
            self.canvas.show()
            self.__threadTrain.stop()
            self.__threadTrain.join()
            self.__threadTrain.exit()
        
        return self.__threadTrain
        
    def stop_trainthread(self):
        trainingthread = self.__threadTrain
        self._async_raise(trainingthread.ident, SystemExit)
        self.StateQueue.put("train stopped.")
        self.train_flag = True
        print 'train stopped.'


改造後的方案,只是在 _async_raise 函式最前面,將tid轉換成了c_long型別。因為傳到API中的型別需要是C的長整形,不然會越界。因為在我的環境中,PID是一個較大的值。

解決方案利用的是python內建API,通過ctypes模組呼叫,線上程中丟出異常,使執行緒退出。

希望我的分享能給各位python程式猿一些幫助。