欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中start和run方法的區(qū)別

 更新時(shí)間:2022年02月13日 08:57:23   作者:小槿12358  
大家好,本篇文章主要講的是python中start和run方法的區(qū)別,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下

結(jié)論:啟動(dòng)線程,如果對(duì)target進(jìn)行賦值,并且沒有重寫run方法,則線程start的時(shí)候會(huì)直接調(diào)用target中對(duì)應(yīng)的方法

具體代碼如下:
1、初始化一個(gè)線程

threading.Thread.__init__(self,target=thread_run())

def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, *, daemon=None):
        assert group is None, "group argument must be None for now"
        if kwargs is None:
            kwargs = {}
        self._target = target
        self._name = str(name or _newname())
        self._args = args
        self._kwargs = kwargs

2、調(diào)用start啟動(dòng)線程
最終調(diào)用_start_new_thread方法,self._bootstrap作為傳參

thread1.start()
    def start(self):
        if not self._initialized:
            raise RuntimeError("thread.__init__() not called")
        if self._started.is_set():
            raise RuntimeError("threads can only be started once")
        with _active_limbo_lock:
            _limbo[self] = self
        try:
            _start_new_thread(self._bootstrap, ())
        except Exception:
            with _active_limbo_lock:
                del _limbo[self]
            raise
        self._started.wait()

3、_start_new_thread等同于啟動(dòng)一個(gè)新線程,并在新線程中調(diào)用回調(diào)函數(shù)

_start_new_thread = _thread.start_new_thread
def start_new_thread(function: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any] = ...) -> int: ...

4、執(zhí)行的回調(diào)函數(shù)就是上文傳入的self._bootstrap, _bootstrap方法直接調(diào)用_bootstrap_inner(),而bootstrap_inner則調(diào)用run方法

def _bootstrap_inner(self):
    try:
        self._set_ident()
        self._set_tstate_lock()
        if _HAVE_THREAD_NATIVE_ID:
            self._set_native_id()
        self._started.set()
        with _active_limbo_lock:
            _active[self._ident] = self
            del _limbo[self]

        if _trace_hook:
            _sys.settrace(_trace_hook)
        if _profile_hook:
            _sys.setprofile(_profile_hook)
        try:
            self.run()

5、最終調(diào)用run方法

 def run(self):
        try:
            if self._target:
                self._target(*self._args, **self._kwargs)
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self._target, self._args, self._kwargs

結(jié)論:
如果run方法被重寫,則直接調(diào)用重寫的run方法
如果run方法沒有被重寫,并且target被定義,則會(huì)直接調(diào)用線程創(chuàng)建時(shí)候的target方法,否則什么也不做

此處遇到一問題:
指定target參數(shù),在執(zhí)行過程中,打印的進(jìn)程名mainthread(主進(jìn)程),而不是之前所賦的進(jìn)程名
threading.Thread.init(self,target=thread_run())
分析后發(fā)現(xiàn)賦予target的是執(zhí)行的函數(shù)體,因此會(huì)先執(zhí)行thread_run函數(shù),執(zhí)行結(jié)束后,將thread_run的返回值賦給了target,因?yàn)閠hread_run沒有返回值,因此target的值是None,如果此時(shí)沒有重寫run函數(shù),那么線程什么都不會(huì)做。 thread_run的執(zhí)行是在主線程,而不是我們所認(rèn)為的在子線程中執(zhí)行thread_run

def thread_run():
    print ("overwrite: 開始線程:" + threading.current_thread().name)
    time.sleep(2)
    print ("overwrite: 退出線程:" + threading.current_thread().name)

class myThread (threading.Thread):
    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self,target=thread_run())
        self.threadID = threadID
        self.name = name
        self.delay = delay
        thread1.start()

thread1.join()
print ("退出主線程")

運(yùn)行結(jié)果:

overwrite: 開始線程:MainThread
overwrite: 退出線程:MainThread
退出主線程

到此這篇關(guān)于python中start和run方法的區(qū)別的文章就介紹到這了,更多相關(guān)python start和run方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 多進(jìn)程共享全局變量之Manager()詳解

    python 多進(jìn)程共享全局變量之Manager()詳解

    這篇文章主要介紹了python 多進(jìn)程共享全局變量之Manager()詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python?計(jì)算機(jī)視覺編程進(jìn)階之OpenCV?圖像銳化及邊緣檢測

    Python?計(jì)算機(jī)視覺編程進(jìn)階之OpenCV?圖像銳化及邊緣檢測

    計(jì)算機(jī)視覺這種技術(shù)可以將靜止圖像或視頻數(shù)據(jù)轉(zhuǎn)換為一種決策或新的表示。所有這樣的轉(zhuǎn)換都是為了完成某種特定的目的而進(jìn)行的,本篇我們來學(xué)習(xí)下如何對(duì)圖像進(jìn)行銳化處理以及如何進(jìn)行邊緣檢測
    2021-11-11
  • Python連接PostgreSQL數(shù)據(jù)庫的方法

    Python連接PostgreSQL數(shù)據(jù)庫的方法

    大家應(yīng)該都有所了解,python可以操作多種數(shù)據(jù)庫,諸如SQLite、MySql、PostgreSQL等,這里不對(duì)所有的數(shù)據(jù)庫操作方法進(jìn)行贅述,只針對(duì)目前項(xiàng)目中用到的PostgreSQL做一下簡單介紹,主要是Python連接PostgreSQL數(shù)據(jù)庫的方法。有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • 詳解Python字典的操作

    詳解Python字典的操作

    在本文里小編給大家整理了關(guān)于Python字典的相關(guān)知識(shí)點(diǎn)以及操作方式,有興趣的朋友們學(xué)習(xí)下。
    2019-03-03
  • Python 多核并行計(jì)算的示例代碼

    Python 多核并行計(jì)算的示例代碼

    本篇文章主要介紹了Python 多核并行計(jì)算的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • OpenCV學(xué)習(xí)之圖像加噪與濾波的實(shí)現(xiàn)詳解

    OpenCV學(xué)習(xí)之圖像加噪與濾波的實(shí)現(xiàn)詳解

    這篇文章主要為大家詳細(xì)介紹了OpenCV中圖像的加噪與濾波操作的相關(guān)資料,文中的示例代碼簡潔易懂,具有一定的借鑒價(jià)值,需要的可以參考一下
    2023-02-02
  • 利用 python 對(duì)目錄下的文件進(jìn)行過濾刪除

    利用 python 對(duì)目錄下的文件進(jìn)行過濾刪除

    這篇文章主要給大家介紹了關(guān)于如何利用 python 對(duì)目錄下的文件進(jìn)行過濾刪除的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Python無權(quán)點(diǎn)文件轉(zhuǎn)化成鄰接矩陣方式

    Python無權(quán)點(diǎn)文件轉(zhuǎn)化成鄰接矩陣方式

    這篇文章主要介紹了Python無權(quán)點(diǎn)文件轉(zhuǎn)化成鄰接矩陣方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 使用 Python 的 pprint庫格式化和輸出列表和字典的方法

    使用 Python 的 pprint庫格式化和輸出列表和字典的方法

    pprint是"pretty-print"的縮寫,使用 Python 的標(biāo)準(zhǔn)庫 pprint 模塊,以干凈的格式輸出和顯示列表和字典等對(duì)象,這篇文章主要介紹了如何使用 Python 的 pprint庫格式化和輸出列表和字典,需要的朋友可以參考下
    2023-05-05
  • python繪制熱力圖heatmap

    python繪制熱力圖heatmap

    這篇文章主要為大家詳細(xì)介紹了python繪制熱力圖heatmap,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11

最新評(píng)論