Python:線程之定位與銷燬

背景

開工前我就以爲有什麼不太對勁,感受要背鍋。這可不,上班第三天就捅鍋了。html

咱們有個了不得的後臺程序,能夠動態加載模塊,並以線程方式運行,經過這種形式實現插件的功能。而模塊更新時候,後臺程序自身不會退出,只會將模塊對應的線程關閉、更新代碼再啓動,6 得不行。python

因而乎我就寫了個模塊準備大展身手,結果忘記寫退出函數了,致使每次更新模塊都新建立一個線程,除非重啓那個程序,不然那些線程就一直苟活着。segmentfault

這可不行啊,得想個辦法清理呀,要否則怕是要炸了。api

那麼怎麼清理呢?我能想到的就是兩步走:多線程

  1. 找出須要清理的線程號 tid;
  2. 銷燬它們;

找出線程ID

和平時的故障排查類似,先經過 ps 命令看看目標進程的線程狀況,由於已是 setName 設置過線程名,因此正常來講應該是看到對應的線程的。 直接用下面代碼來模擬這個線程:併發

Python 版本的多線程async

#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()

輸出:ide

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
  PID  SPID TTY          TIME CMD
 5613  5613 pts/2    00:00:00 python
 5613  5614 pts/2    00:00:00 python
 5613  5615 pts/2    00: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
  PID  SPID TTY          TIME CMD
 5575  5575 pts/2    00:00:00 a.out
 5575  5576 pts/2    00:00:00 Love_test_1
 5575  5577 pts/2    00: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

    @name.setter
    def name(self, name):
        assert self.__initialized, "Thread.__init__() not called"
        self.__name = str(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-test1Thread-test2 都是打印出當前的 pid、線程 id 和 線程名字,而後 3s 後退出,這個是想模擬線程正常退出。

Checker 線程則是每秒經過 threading.enumerate 輸出當前進程內全部活躍的線程。

能夠明顯看到一開始是能夠看到 Thread-test1Thread-test2的信息,當它倆退出以後就只剩下 MainThreadChecker 自身而已了。

銷燬指定線程

既然能拿到名字和線程 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,觸發指定線程的異常,讓其能夠自動退出;

clipboard.png

萬不得已真不要用這種方法,有必定機率觸發不可描述的問題。切記!別問我爲何會知道...

爲何中止線程這麼難

多線程自己設計就是在進程下的協做併發,是調度的最小單元,線程間分食着進程的資源,因此會有許多鎖機制和狀態控制。

若是使用強制手段幹掉線程,那麼很大概率出現意想不到的bug。 並且最重要的鎖資源釋放可能也會出現意想不到問題。

咱們甚至也沒法經過信號殺死進程那樣直接殺線程,由於 kill 只有對付進程才能達到咱們的預期,而對付線程明顯不能夠,無論殺哪一個線程,整個進程都會退出!

而由於有 GIL,使得不少童鞋都以爲 Python 的線程是Python 自行實現出來的,並不是實際存在,Python 應該能夠直接銷燬吧?

然而事實上 Python 的線程都是貨真價實的線程!

什麼意思呢?Python 的線程是操做系統經過 pthread 建立的原生線程。Python 只是經過 GIL 來約束這些線程,來決定何時開始調度,比方說運行了多少個指令就交出 GIL,至於誰奪得花魁,得聽操做系統的。

若是是單純的線程,其實系統是有辦法終止的,好比: pthread_exit,pthread_killpthread_cancel, 詳情可看:https://www.cnblogs.com/Creat...

很惋惜的是: Python 層面並無這些方法的封裝!個人天,好氣!可能人家以爲,線程就該溫柔對待吧。

如何溫柔退出線程

想要溫柔退出線程,其實差很少就是一句廢話了~

要麼運行完退出,要麼設置標誌位,時常檢查標記位,該退出的就退出咯。

擴展

《如何正確的終止正在運行的子線程》:https://www.cnblogs.com/Creat...
《不要粗暴的銷燬python線程》:http://xiaorui.cc/2017/02/22/...

歡迎各位大神指點交流, QQ討論羣: 258498217
轉載請註明來源: https://segmentfault.com/a/11...

相關文章
相關標籤/搜索