Python:執行緒之定位與銷燬
背景
開工前我就覺得有什麼不太對勁,感覺要背鍋。這可不,上班第三天就捅鍋了。
我們有個了不起的後臺程式,可以動態載入模組,並以執行緒方式執行,通過這種形式實現外掛的功能。而模組更新時候,後臺程式自身不會退出,只會將模組對應的執行緒關閉、更新程式碼再啟動,6 得不行。
於是乎我就寫了個模組準備大展身手,結果忘記寫退出函數了,導致每次更新模組都新建立一個執行緒,除非重啟那個程式,否則那些執行緒就一直苟活著。
這可不行啊,得想個辦法清理呀,要不然怕是要炸了。
那麼怎麼清理呢?我能想到的就是兩步走:
- 找出需要清理的執行緒號 tid;
- 銷燬它們;
找出執行緒ID
和平時的故障排查相似,先通過 ps 命令看看目標程序的執行緒情況,因為已經是 setName 設定過執行緒名,所以正常來說應該是看到對應的執行緒的。 直接用下面程式碼來模擬這個執行緒:
Python 版本的多執行緒
#coding: utf8 import threading import os import time def tt(): info = threading.currentThread() while True: print 'pid: ', os.getpid() print info.name, info.ident time.sleep(3) t1 = threading.Thread(target=tt) t1.setName('OOOOOPPPPP') t1.setDaemon(True) t1.start() t2 = threading.Thread(target=tt) t2.setName('EEEEEEEEE') t2.setDaemon(True) t2.start() t1.join() t2.join()
輸出:
root@10-46-33-56:~# python t.py pid:5613 OOOOOPPPPP 139693508122368 pid:5613 EEEEEEEEE 139693497632512 ...
可以看到在 Python 裡面輸出的執行緒名就是我們設定的那樣,然而 Ps 的結果卻是令我懷疑人生:
root@10-46-33-56:~# ps -Tp 5613 PIDSPID TTYTIME CMD 56135613 pts/200:00:00 python 56135614 pts/200:00:00 python 56135615 pts/200:00:00 python
正常來說不該是這樣呀,我有點迷了,難道我一直都是記錯了?用別的語言版本的多執行緒來測試下:
C 版本的多執行緒
#include<stdio.h> #include<sys/syscall.h> #include<sys/prctl.h> #include<pthread.h> void *test(void *name) { pid_t pid, tid; pid = getpid(); tid = syscall(__NR_gettid); char *tname = (char *)name; // 設定執行緒名字 prctl(PR_SET_NAME, tname); while(1) { printf("pid: %d, thread_id: %u, t_name: %s\n", pid, tid, tname); sleep(3); } } int main() { pthread_t t1, t2; void *ret; pthread_create(&t1, NULL, test,(void *)"Love_test_1"); pthread_create(&t2, NULL, test,(void *)"Love_test_2"); pthread_join(t1, &ret); pthread_join(t2, &ret); }
輸出:
root@10-46-33-56:~# gcc t.c -lpthread && ./a.out pid: 5575, thread_id: 5577, t_name: Love_test_2 pid: 5575, thread_id: 5576, t_name: Love_test_1 pid: 5575, thread_id: 5577, t_name: Love_test_2 pid: 5575, thread_id: 5576, t_name: Love_test_1 ...
用 PS 命令再次驗證:
root@10-46-33-56:~# ps -Tp 5575 PIDSPID TTYTIME CMD 55755575 pts/200:00:00 a.out 55755576 pts/200:00:00 Love_test_1 55755577 pts/200:00:00 Love_test_2
這個才是正確嘛,執行緒名確實是可以通過 Ps 看出來的嘛!
不過為啥 Python 那個看不到呢?既然是通過 setName
設定執行緒名的,那就看看定義咯:
[threading.py] class Thread(_Verbose): ... @property def name(self): """A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. The initial name is set by the constructor. """ assert self.__initialized, "Thread.__init__() not called" return self.__name def setName(self, name): self.name = name ...
看到這裡其實只是在 Thread
物件的屬性設定了而已,並沒有動到根本,那肯定就是看不到咯~
這樣看起來,我們已經沒辦法通過 ps
或者 /proc/
這類手段在外部搜尋 python 執行緒名了,所以我們只能在 Python 內部來解決。
於是問題就變成了,怎樣在 Python 內部拿到所有正在執行的執行緒呢?
threading.enumerate
可以完美解決這個問題!Why?
Because 在下面這個函式的 doc 裡面說得很清楚了,返回所有活躍的 執行緒物件 ,不包括終止和未啟動的。
[threading.py] def enumerate(): """Return a list of all Thread objects currently alive. The list includes daemonic threads, dummy thread objects created by current_thread(), and the main thread. It excludes terminated threads and threads that have not yet been started. """ with _active_limbo_lock: return _active.values() + _limbo.values()
因為拿到的是 Thread 的物件,所以我們通過這個能到該執行緒相關的資訊!
請看完整程式碼示例:
#coding: utf8 import threading import os import time def get_thread(): pid = os.getpid() while True: ts = threading.enumerate() print '------- Running threads On Pid: %d -------' % pid for t in ts: print t.name, t.ident print time.sleep(1) def tt(): info = threading.currentThread() pid = os.getpid() while True: print 'pid: {}, tid: {}, tname: {}'.format(pid, info.name, info.ident) time.sleep(3) return t1 = threading.Thread(target=tt) t1.setName('Thread-test1') t1.setDaemon(True) t1.start() t2 = threading.Thread(target=tt) t2.setName('Thread-test2') t2.setDaemon(True) t2.start() t3 = threading.Thread(target=get_thread) t3.setName('Checker') t3.setDaemon(True) t3.start() t1.join() t2.join() t3.join()
輸出:
root@10-46-33-56:~# python t_show.py pid: 6258, tid: Thread-test1, tname: 139907597162240 pid: 6258, tid: Thread-test2, tname: 139907586672384 ------- Running threads On Pid: 6258 ------- MainThread 139907616806656 Thread-test1 139907597162240 Checker 139907576182528 Thread-test2 139907586672384 ------- Running threads On Pid: 6258 ------- MainThread 139907616806656 Thread-test1 139907597162240 Checker 139907576182528 Thread-test2 139907586672384 ------- Running threads On Pid: 6258 ------- MainThread 139907616806656 Thread-test1 139907597162240 Checker 139907576182528 Thread-test2 139907586672384 ------- Running threads On Pid: 6258 ------- MainThread 139907616806656 Checker 139907576182528 ...
程式碼看起來有點長,但是邏輯相當簡單, Thread-test1
和 Thread-test2
都是打印出當前的 pid、執行緒 id 和 執行緒名字,然後 3s 後退出,這個是想模擬執行緒正常退出。
而 Checker
執行緒則是每秒通過 threading.enumerate
輸出當前程序內所有活躍的執行緒。
可以明顯看到一開始是可以看到 Thread-test1
和 Thread-test2
的資訊,當它倆退出之後就只剩下 MainThread
和 Checker
自身而已了。
銷燬指定執行緒
既然能拿到名字和執行緒 id,那我們也就能幹掉指定的執行緒了!
假設現在 Thread-test2
已經黑化,發瘋了,我們需要制止它,那我們就可以通過這種方式解決了:
在上面的程式碼基礎上,增加和補上下列程式碼:
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: ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit) def get_thread(): pid = os.getpid() while True: ts = threading.enumerate() print '------- Running threads On Pid: %d -------' % pid for t in ts: print t.name, t.ident, t.is_alive() if t.name == 'Thread-test2': print 'I am go dying! Please take care of yourself and drink more hot water!' stop_thread(t) print time.sleep(1)
輸出
root@10-46-33-56:~# python t_show.py pid: 6362, tid: 139901682108160, tname: Thread-test1 pid: 6362, tid: 139901671618304, tname: Thread-test2 ------- Running threads On Pid: 6362 ------- MainThread 139901706389248 True Thread-test1 139901682108160 True Checker 139901661128448 True Thread-test2 139901671618304 True Thread-test2: I am go dying. Please take care of yourself and drink more hot water! ------- Running threads On Pid: 6362 ------- MainThread 139901706389248 True Thread-test1 139901682108160 True Checker 139901661128448 True Thread-test2 139901671618304 True Thread-test2: I am go dying. Please take care of yourself and drink more hot water! pid: 6362, tid: 139901682108160, tname: Thread-test1 ------- Running threads On Pid: 6362 ------- MainThread 139901706389248 True Thread-test1 139901682108160 True Checker 139901661128448 True // Thread-test2 已經不在了
一頓操作下來,雖然我們這樣對待 Thread-test2
,但它還是關心著我們: 多喝熱水 ,
PS: 熱水雖好,八杯足矣,請勿貪杯哦。
書回正傳,上述的方法是極為粗暴的,為什麼這麼說呢?
因為它的原理是:利用 Python 內建的 API,觸發指定執行緒的異常,讓其可以自動退出;
為什麼停止執行緒這麼難
多執行緒本身設計就是在程序下的協作併發,是排程的最小單元,執行緒間分食著程序的資源,所以會有許多鎖機制和狀態控制。
如果使用強制手段幹掉執行緒,那麼很大機率出現意想不到的bug。 而且最重要的鎖資源釋放可能也會出現意想不到問題。
我們甚至也無法通過訊號殺死程序那樣直接殺執行緒,因為 kill 只有對付程序才能達到我們的預期,而對付執行緒明顯不可以,不管殺哪個執行緒,整個程序都會退出!
而因為有 GIL,使得很多童鞋都覺得 Python 的執行緒是Python 自行實現出來的,並非實際存在,Python 應該可以直接銷燬吧?
然而事實上 Python 的執行緒都是貨真價實的執行緒!
什麼意思呢?Python 的執行緒是作業系統通過 pthread 建立的原生執行緒。Python 只是通過 GIL 來約束這些執行緒,來決定什麼時候開始排程,比方說運行了多少個指令就交出 GIL,至於誰奪得花魁,得聽作業系統的。
如果是單純的執行緒,其實系統是有辦法終止的,比如: pthread_exit
, pthread_kill
或 pthread_cancel
, 詳情可看: https://www.cnblogs.com/Creat...
很可惜的是: Python 層面並沒有這些方法的封裝!我的天,好氣!可能人家覺得,執行緒就該溫柔對待吧。
如何溫柔退出執行緒
想要溫柔退出執行緒,其實差不多就是一句廢話了~
要麼執行完退出,要麼設定標誌位,時常檢查標記位,該退出的就退出咯。
擴充套件
《如何正確的終止正在執行的子執行緒》: https://www.cnblogs.com/Creat...
《不要粗暴的銷燬python執行緒》: http://xiaorui.cc/2017/02/22/...