python 歸納 (十一)_線程_threading.Thread

來源:互聯網
上載者:User

標籤:cal   import   color   子進程   進程   style   highlight   %s   xxxx   

總結:
  1. 預設父線程跑完,子線程並不會馬上退出,不像 thread.start_threadXXXX
  2. 父線程跑完了,並沒有退出,一直在那裡
  3. 線程啟動速度很快,不佔多少開銷,不到1毫秒
代碼:
# -*- coding: utf-8 -*-"""學習 並發linux下執行"""from threading import Threadfrom multiprocessing import Processimport timeimport osimport psutildef work():    print(‘%s,%f: sub begin %d‘ % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid()))    print "%s,%f: sub %d,%d,%d" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())    time.sleep(5)    print "%s,%f: sub %d,%d,%d" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())    print(‘%s,%f: sub end %d‘ % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid()))if __name__ == ‘__main__‘:    print "%s,%f: main begin" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time())    print "%s,%f: main %d,%d,%d" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())    # 開啟子線程    t=Thread(target=work)    t.start()    print  ‘%s,%f: main 線程‘ %  (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time())    print "%s,%f: main %d,%d,%d" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())    # 開啟子進程    # t=Process(target=work)    # t.start()    # print "\nmain %d,%d,%d" % (os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())    # print(‘\nmain 進程‘)    print "%s,%f: main end" % (time.strftime(‘%M:%S‘,time.localtime(time.time())),time.time())

  

輸出:

格式說明:

小時 :秒,時間戳記,     <當前進程ID,  [父 進程ID,當前進程線程數量]>

[email protected]:~/python# python test_threadprocess1.py

----------------------------------------------------------

03:04,1536397384.349529: main begin
03:04,1536397384.349605: main 3604,2692,1
03:04,1536397384.350838: sub begin 3604
03:04,1536397384.350939: main 線程
03:04,1536397384.351009: sub 3604,2692,2
03:04,1536397384.351310: main 3604,2692,2
03:04,1536397384.352097: main end
03:09,1536397389.357353: sub 3604,2692,2   

03:09,1536397389.358372: sub end 3604

----------------------------------------------------------

輸出解讀:
  1. 黃色 標記的中間 輸出部分 ,是啟動 子線程期間,這段 時間非常快,說明啟動線程的速度很快
  2. 紅色標記,此時main線程的邏輯 代碼跑完了,但是還是顯示有2個線程,對比開始,說明main線程並 沒有退出 

 

python 歸納 (十一)_線程_threading.Thread

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.