Python學習之路:守護線程

來源:互聯網
上載者:User

標籤:sleep   --   end   int   join   def   退出   主線程   imp   

守護線程:只起到守護主線程的作用,主線程退出守護線程也跟著退出。

 1 import threading,time 2  3 def run(n): 4     print("task",n) 5     time.sleep(2) 6     print("task done",n) 7  8 start_time = time.time() 9 t_objs =  []10 for i in range(50):11     t = threading.Thread(target=run,args=("t-%s"%i,))12     t.setDaemon(True)#把當前線程設定為守護線程13     t.start()14     t_objs.append(t)#為了不阻塞後麵線程的啟動,不在這裡join,先放到一個列表裡15 16 # for t in t_objs:#迴圈線程執行個體列表,等待所有線程執行完畢17 #     t.join()18 19 print("------all threads has finished....",threading.current_thread(),)20 print("cost:",time.time()-start_time)21 22     # t1 = threading.Thread(target=run,args=("t1",))23     # t2 = threading.Thread(target=run,args=("t2",))24     # t1.start()25     # t2.start()26 27 # run("t1")28 # run("t2")
守護線程練習

 

Python學習之路:守護線程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.