小編給大家分享一下python中threading實(shí)現(xiàn)線程的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
成都創(chuàng)新互聯(lián)公司成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站開(kāi)發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),自適應(yīng)網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:18980820575
過(guò)程說(shuō)明
1、從Thread類構(gòu)成子類。覆蓋方法根據(jù)需要提供參數(shù)。
2、重寫線程的業(yè)務(wù)邏輯。
3、新的Thread子類一旦被定義,就必須對(duì)其進(jìn)行實(shí)例化,以啟動(dòng)新的線程。然后,調(diào)用方法啟動(dòng)它。最后,它將調(diào)用方法來(lái)執(zhí)行業(yè)務(wù)邏輯。
實(shí)例
#Python 多線程示例打印當(dāng)前日期。 #1. 使用 threading.Thread 類定義子類。 #2. 實(shí)例化子類并觸發(fā)線程。 import threading import datetime class myThread (threading.Thread): def __init__(self, name, counter): threading.Thread.__init__(self) self.threadID = counter self.name = name self.counter = counter def run(self): print("\nStarting " + self.name) print_date(self.name, self.counter) print("Exiting " + self.name) def print_date(threadName, counter): datefields = [] today = datetime.date.today() datefields.append(today) print("{}[{}]: {}".format( threadName, counter, datefields[0] )) # 創(chuàng)建新線程 thread1 = myThread("Thread", 1) thread2 = myThread("Thread", 2) # 啟動(dòng)新線程 thread1.start() thread2.start() thread1.join() thread2.join() print("\nExiting the Program!!!")
以上是“python中threading實(shí)現(xiàn)線程的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:python中threading實(shí)現(xiàn)線程的示例分析
網(wǎng)頁(yè)URL:http://www.chinadenli.net/article18/pigsdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、網(wǎng)站制作、移動(dòng)網(wǎng)站建設(shè)、微信小程序、做網(wǎng)站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)