最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python線程創(chuàng)建和終止實(shí)例代碼

 更新時(shí)間:2018年01月20日 13:54:05   作者:claireyuancy  
這篇文章主要介紹了Python線程創(chuàng)建和終止實(shí)例代碼,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

python主要是通過thread和threading這兩個(gè)模塊來實(shí)現(xiàn)多線程支持。

python的thread模塊是比較底層的模塊,python的threading模塊是對(duì)thread做了一些封裝,能夠更加方便的被使用??墒莗ython(cpython)因?yàn)镚IL的存在無法使用threading充分利用CPU資源,假設(shè)想充分發(fā)揮多核CPU的計(jì)算能力須要使用multiprocessing模塊(Windows下使用會(huì)有諸多問題)。

假設(shè)在對(duì)線程應(yīng)用有較高的要求時(shí)能夠考慮使用Stackless Python來完畢。Stackless Python是Python的一個(gè)改動(dòng)版本號(hào),對(duì)多線程編程有更好的支持,提供了對(duì)微線程的支持。微線程是輕量級(jí)的線程,在多個(gè)線程間切換所需的時(shí)間很多其它,占用資源也更少。

通過threading模塊創(chuàng)建新的線程有兩種方法:一種是通過threading.Thread(Target=executable Method)-即傳遞給Thread對(duì)象一個(gè)可運(yùn)行方法(或?qū)ο螅?另外一種是繼承threading.Thread定義子類并重寫run()方法。另外一種方法中,唯一必須重寫的方法是run(),可依據(jù)需要決定是否重寫__init__()。值得注意的是,若要重寫__init__(),父類的__init__()必需要在函數(shù)第一行調(diào)用,否則會(huì)觸發(fā)錯(cuò)誤“AssertionError: Thread.__init__() not called”

Python threading模塊不同于其它語言之處在于它沒有提供線程的終止方法,通過Python threading.Thread()啟動(dòng)的線程彼此是獨(dú)立的。若在線程A中啟動(dòng)了線程B,那么A、B是彼此獨(dú)立執(zhí)行的線程。若想終止線程A的同一時(shí)候強(qiáng)力終止線程B。一個(gè)簡單的方法是通過在線程A中調(diào)用B.setDaemon(True)實(shí)現(xiàn)。

但這樣帶來的問題是:線程B中的資源(打開的文件、傳輸數(shù)據(jù)等)可能會(huì)沒有正確的釋放。所以setDaemon()并不是一個(gè)好方法,更為妥當(dāng)?shù)姆绞绞峭ㄟ^Event機(jī)制。以下這段程序體現(xiàn)了setDaemon()和Event機(jī)制終止子線程的差別。

import threading 
import time 
class mythread(threading.Thread): 
 def __init__(self,stopevt = None,File=None,name = 'subthread',Type ='event'): 
  threading.Thread.__init__(self) 
  self.stopevt = stopevt 
  self.name = name 
  self.File = File 
  self.Type = Type 
   
     
 def Eventrun(self): 
  while not self.stopevt.isSet(): 
   print self.name +' alive\n' 
   time.sleep(2) 
  if self.File: 
   print 'close opened file in '+self.name+'\n' 
   self.File.close() 
  print self.name +' stoped\n' 
  
 def Daemonrun(self): 
  D = mythreadDaemon(self.File) 
  D.setDaemon(True) 
  while not self.stopevt.isSet(): 
   print self.name +' alive\n' 
   time.sleep(2) 
  print self.name +' stoped\n' 
 def run(self): 
  if self.Type == 'event': self.Eventrun() 
  else: self.Daemonrun() 
class mythreadDaemon(threading.Thread): 
 def __init__(self,File=None,name = 'Daemonthread'): 
  threading.Thread.__init__(self) 
  self.name = name 
  self.File = File 
 def run(self): 
  while True: 
   print self.name +' alive\n' 
   time.sleep(2) 
  if self.File: 
   print 'close opened file in '+self.name+'\n' 
   self.File.close() 
  print self.name +' stoped\n' 
   
def evtstop(): 
 stopevt = threading.Event() 
 FileA = open('testA.txt','w') 
 FileB = open('testB.txt','w') 
 A = mythread(stopevt,FileA,'subthreadA') 
 B = mythread(stopevt,FileB,'subthreadB') 
 print repr(threading.currentThread())+'alive\n' 
 print FileA.name + ' closed?
 '+repr(FileA.closed)+'\n' 
 print FileB.name + ' closed? '+repr(FileB.closed)+'\n' 
 A.start() 
 B.start() 
 time.sleep(1) 
 print repr(threading.currentThread())+'send stop signal\n' 
 stopevt.set() 
 A.join() 
 B.join() 
 print repr(threading.currentThread())+'stoped\n' 
 print 'after A stoped, '+FileA.name + ' closed? '+repr(FileA.closed)+'\n' 
 print 'after A stoped, '+FileB.name + ' closed?

 '+repr(FileB.closed)+'\n' 
def daemonstop(): 
 stopevt = threading.Event() 
 FileA = open('testA.txt','r') 
 A = mythread(stopevt,FileA,'subthreadA',Type = 'Daemon') 
 print repr(threading.currentThread())+'alive\n' 
 print FileA.name + ' closed?

 '+repr(FileA.closed)+'\n' 
 A.start() 
 time.sleep(1) 
 stopevt.set() 
 A.join() 
 print repr(threading.currentThread())+'stoped\n' 
 print 'after A stoped, '+FileA.name + ' closed? '+repr(FileA.closed)+'\n' 
 if not FileA.closed: 
  print 'You see the differents, the resource in subthread may not released with setDaemon()' 
  FileA.close() 
if __name__ =='__main__': 
 print '-------stop subthread example with Event:----------\n' 
 evtstop() 
 print '-------Daemon stop subthread example :----------\n' 
 daemonstop() 

執(zhí)行結(jié)果是:

-------stop subthread example with Event:---------- 
<_MainThread(MainThread, started 2436)>alive 
testA.txt closed?
 False 
testB.txt closed? False 
subthreadA alive 
subthreadB alive 
 
<_MainThread(MainThread, started 2436)>send stop signal 
close opened file in subthreadA 
close opened file in subthreadB 
 
subthreadA stoped 
subthreadB stoped 
 
<_MainThread(MainThread, started 2436)>stoped 
after A stoped, testA.txt closed? True 
after A stoped, testB.txt closed?

 True 
-------Daemon stop subthread example :---------- 
<_MainThread(MainThread, started 2436)>alive 
testA.txt closed?

 False 
subthreadA alive 
subthreadA stoped 
<_MainThread(MainThread, started 2436)>stoped 
after A stoped, testA.txt closed? False 
You see the differents, the resource in subthread may not released with setDaemon() 

總結(jié)

以上就是本文關(guān)于Python線程創(chuàng)建和終止實(shí)例代碼的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法

    Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法

    這篇文章主要介紹了Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Pytorch教程內(nèi)置模型源碼實(shí)現(xiàn)

    Pytorch教程內(nèi)置模型源碼實(shí)現(xiàn)

    本文是關(guān)于Pytorch教程文章,本篇主要為教大家Pytorch內(nèi)置模型源碼實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-09-09
  • Django框架實(shí)現(xiàn)的分頁demo示例

    Django框架實(shí)現(xiàn)的分頁demo示例

    這篇文章主要介紹了Django框架實(shí)現(xiàn)的分頁demo,結(jié)合實(shí)例形式分析了Django框架分頁的步驟、原理、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-05-05
  • Python如何對(duì)圖像補(bǔ)全并分割成多塊補(bǔ)丁

    Python如何對(duì)圖像補(bǔ)全并分割成多塊補(bǔ)丁

    這篇文章主要介紹了Python如何對(duì)圖像補(bǔ)全并分割成多塊補(bǔ)丁,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 使用__init__.py將文件夾設(shè)置成Python模塊示例詳解

    使用__init__.py將文件夾設(shè)置成Python模塊示例詳解

    這篇文章主要為大家介紹了使用__init__.py將文件夾設(shè)置成Python模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 將python打包后的exe還原成py

    將python打包后的exe還原成py

    這篇文章主要介紹了將python打包后的exe還原成py,利用pyinstxtractor.py?拆包(解壓)工具,將exe文件解壓成一個(gè)文件夾<BR>uncompyle6?pyc反編譯工具,需要的朋友可以參考一下
    2022-01-01
  • python如何將圖片生成視頻MP4

    python如何將圖片生成視頻MP4

    這篇文章主要介紹了python如何將圖片生成視頻MP4問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python語言內(nèi)置數(shù)據(jù)類型

    Python語言內(nèi)置數(shù)據(jù)類型

    這篇文章主要介紹了Python語言中數(shù)據(jù)類型支持得運(yùn)算符,Python語言提供了豐富的內(nèi)置數(shù)據(jù)類型。用于有效的處理各種類型的數(shù)據(jù),下文將介紹到其數(shù)據(jù)類型支持的運(yùn)算符等相關(guān)內(nèi)容,需要的朋友可以參考一下
    2022-02-02
  • 淺析Python數(shù)據(jù)處理

    淺析Python數(shù)據(jù)處理

    本篇文章給大家分享了關(guān)于Python數(shù)據(jù)處理的相關(guān)內(nèi)容以及要點(diǎn)解釋,對(duì)此知識(shí)點(diǎn)有興趣的朋友可以參考學(xué)習(xí)下。
    2018-05-05
  • Python使用pip安裝pySerial串口通訊模塊

    Python使用pip安裝pySerial串口通訊模塊

    這篇文章主要為大家詳細(xì)介紹了Python使用pip安裝pySerial串口通訊模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評(píng)論

东丽区| 东乡族自治县| 柞水县| 乐陵市| 邮箱| 凌云县| 贵港市| 平江县| 宿迁市| 益阳市| 南靖县| 大方县| 玉溪市| 乌鲁木齐县| 梅河口市| 泾川县| 双江| 城口县| 休宁县| 达拉特旗| 南通市| 玉环县| 休宁县| 曲靖市| 旬邑县| 宜宾市| 松潘县| 乌鲁木齐县| 旅游| 揭东县| 梓潼县| 利川市| 黄陵县| 都安| 宁乡县| 汉阴县| 鄯善县| 盐池县| 柞水县| 岳池县| 曲松县|