溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

退出python多線程爬蟲的方法有哪些

發布時間:2020-08-07 11:21:46 來源:億速云 閱讀:195 作者:小新 欄目:編程語言

小編給大家分享一下退出python多線程爬蟲的方法有哪些,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

解決方案 · 壹

一個比較nice的方式就是每個線程都帶一個退出請求標志,在線程里面間隔一定的時間來檢查一次,看是不是該自己離開了!

import threading
class StoppableThread(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""
    def __init__(self):
        super(StoppableThread, self).__init__()
        self._stop_event = threading.Event()
    def stop(self):
        self._stop_event.set()
    def stopped(self):
        return self._stop_event.is_set()

在這部分代碼所示,當你想要退出線程的時候你應當顯示調用stop()函數,并且使用join()函數來等待線程合適地退出。線程應當周期性地檢測停止標志。

然而,還有一些使用場景中你真的需要kill掉一個線程:比如,當你封裝了一個外部庫,但是這個外部庫在長時間調用,因此你想中斷這個過程。

解決方案 · 貳

接下來的方案是允許在python線程里面raise一個Exception(當然是有一些限制的)。

def _async_raise(tid, exctype):
    '''Raises an exception in the threads with id tid'''
    if not inspect.isclass(exctype):
        raise TypeError("Only types can be raised (not instances)")
    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, 0)
        raise SystemError("PyThreadState_SetAsyncExc failed")
class ThreadWithExc(threading.Thread):
    '''A thread class that supports raising exception in the thread from
       another thread.
    '''
    def _get_my_tid(self):
        """determines this (self's) thread id
        CAREFUL : this function is executed in the context of the caller
        thread, to get the identity of the thread represented by this
        instance.
        """
        if not self.isAlive():
            raise threading.ThreadError("the thread is not active")
        # do we have it cached?
        if hasattr(self, "_thread_id"):
            return self._thread_id
        # no, look for it in the _active dict
        for tid, tobj in threading._active.items():
            if tobj is self:
                self._thread_id = tid
                return tid
        # TODO: in python 2.6, there's a simpler way to do : self.ident
        raise AssertionError("could not determine the thread's id")
    def raiseExc(self, exctype):
        """Raises the given exception type in the context of this thread.
        If the thread is busy in a system call (time.sleep(),
        socket.accept(), ...), the exception is simply ignored.
        If you are sure that your exception should terminate the thread,
        one way to ensure that it works is:
            t = ThreadWithExc( ... )
            ...
            t.raiseExc( SomeException )
            while t.isAlive():
                time.sleep( 0.1 )
                t.raiseExc( SomeException )
        If the exception is to be caught by the thread, you need a way to
        check that your thread has caught it.
        CAREFUL : this function is executed in the context of the
        caller thread, to raise an excpetion in the context of the
        thread represented by this instance.
        """
        _async_raise( self._get_my_tid(), exctype )

正如注釋里面描述,這不是啥“靈丹妙藥”,因為,假如線程在python解釋器之外busy,這樣子的話終端異常就抓不到啦~

這個代碼的合理使用方式是:讓線程抓住一個特定的異常然后執行清理操作。這樣的話你就能終端一個任務并能合適地進行清除。

解決方案 · 叁

假如我們要做個啥事情,類似于中斷的方式,那么我們就可以用thread.join方式。

join的原理就是依次檢驗線程池中的線程是否結束,沒有結束就阻塞直到線程結束,如果結束則跳轉執行下一個線程的join函數。

先看看這個:

1. 阻塞主進程,專注于執行多線程中的程序。

2. 多線程多join的情況下,依次執行各線程的join方法,前頭一個結束了才能執行后面一個。

3. 無參數,則等待到該線程結束,才開始執行下一個線程的join。

4. 參數timeout為線程的阻塞時間,如 timeout=2 就是罩著這個線程2s 以后,就不管他了,繼續執行下面的代碼。

# coding: utf-8
# 多線程join
import threading, time  
def doWaiting1():  
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"  
    time.sleep(3)  
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n" 
def doWaiting2():  
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"   
    time.sleep(8)  
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"  
tsk = []    
thread1 = threading.Thread(target = doWaiting1)  
thread1.start()  
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting2)  
thread2.start()  
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"   
for tt in tsk:
    tt.join()
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

默認join方式,也就是不帶參,阻塞模式,只有子線程運行完才運行其他的。

1、 兩個線程在同一時間開啟,join 函數執行。

2、waiting1 線程執行(等待)了3s 以后,結束。

3、waiting2 線程執行(等待)了8s 以后,運行結束。

4、join 函數(返回到了主進程)執行結束。

這里是默認的join方式,是在線程已經開始跑了之后,然后再join的,注意這點,join之后主線程就必須等子線程結束才會返回主線。

join的參數,也就是timeout參數,改為2,即join(2),那么結果就是如下了:

兩個線程在同一時間開啟,join 函數執行。

wating1 線程在執行(等待)了三秒以后,完成。

join 退出(兩個2s,一共4s,36-32=4,無誤)。

waiting2 線程由于沒有在 join 規定的等待時間內(4s)完成,所以自己在后面執行完成。

看完了這篇文章,相信你對退出python多線程爬蟲的方法有哪些有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女