日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python第九天(9-33)

發布時間:2023/12/18 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python第九天(9-33) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  

一:進程

  • 進程概念
    • 進程就是一個程序運行在一個數據集上的一次動態執行過程
    • 進程一般由程序,數據集,進程控制塊組成
    • 進程控制塊:?進程控制塊用來記錄進程的外部特征,描述進程的執行變化過程,系統可以利用它來控制和管理進程,它是系統感知進程存在的唯一標志(通俗講就是記錄進程狀態信息)

二:線程

  • 線程概念
    • 一個程序至少有一個進程,一個進程至少有一個線程
    • 線程是進程的組成部分,線程是最小的執行單位
    • 進程相當于一個容器,是最小的資源單位
    • 線程共用進程的資源,線程是進程中的一個實體
    • 一個標準的線程由線程ID,當前指令指針(PC),寄存器集合和堆棧組成
    • 一個線程可以創建和撤消另一個線程

一:線程的創建與調用(threading中Thread類)

  • 創建一個主線程的子線程對象
    • t1=threading.Thread(target=read)
    • target:指向函數,arg:傳遞函數參數
  • 啟用一個子線程
    • t1.start()
    • 實際start()只是讓線程處于就緒態,開始一個線程時run()方法實現的  
1 import threading2 import time3 def read():4 print("I am reading at %s"%time.ctime())5 time.sleep(3)6 print("i am finish reading at %s"%time.ctime())7 def music():8 print("i am listening music at %s"%time.ctime())9 time.sleep(3) 10 print("i am finish listening music at %s"%time.ctime()) 11 if __name__=="__main__": 12 t1=threading.Thread(target=read)#創建這個程序的主線程的子線程對象 13 #target:指向調用的函數,arg用于傳遞函數中的參數 14 t1.start()#啟用某個線程 15 t2=threading.Thread(target=music) 16 t2.start() 線程執行的理解

?

1.首先t1,t2與主線程開啟,直接輸出 I am reading at Thu Jan 31 12:20:38 2019 I am listening music at Thu Jan 31 12:20:38 2019 Main threading ending at Thu Jan 31 12:20:38 2019...2.倆個進程等待三秒鐘 3.最后輸出 I am finish listening music at Thu Jan 31 12:20:41 2019 I am finish reading at Thu Jan 31 12:20:41 2019

二:join的用法理解

1 import threading2 import time3 def read():4 print("I am reading at %s"%time.ctime(),end="\n")5 time.sleep(3)6 print("I am finish reading at %s"%time.ctime(),end="\n")7 def music():8 print("I am listening music at %s"%time.ctime())9 time.sleep(5) 10 print("I am finish listening music at %s"%time.ctime(),end="\n") 11 if __name__=="__main__": 12 t1=threading.Thread(target=read)#創建這個程序的主線程的子線程對象 13 #target:指向調用的函數,arg用于傳遞函數中的參數 14 t2=threading.Thread(target=music) 15 t1.start() # 啟用某個線程 16 t2.start() 17 #t1.join() 18 t2.join() 19 print(r"main threading ending at %s..."%time.ctime()) join用法的理解
  • 沒有join時,主線程開子線程一起執行
  • 注釋掉t2.join()          
      • 1.執行t1,t2子線程I am reading at Thu Jan 31 13:09:39 2019I am listening music at Thu Jan 31 13:09:39 2019 2.進入等待3秒鐘 3.執行t1子線程和主線程I am finish reading at Thu Jan 31 13:09:42 2019
          main threading ending at Thu Jan 31 13:09:44 2019... 4.進入等待倆秒鐘 5。執行t2I am finish listening music at Thu Jan 31 13:09:44 2019
  • 注釋掉t1.join
      • 1.執行t1,t2子線程I am reading at Thu Jan 31 13:20:50 2019I am listening music at Thu Jan 31 13:20:50 2019 2.進入等待5秒鐘,單線程1只需等待三秒鐘,所以在三秒鐘時會繼續執行ti線程I am finish reading at Thu Jan 31 13:20:53 2019 3.在三秒鐘后在過倆秒鐘執行t2和主線程I am finish listening music at Thu Jan 31 13:23:28 2019main threading ending at Thu Jan 31 13:23:28 2019...       
  • 都不注釋
      • 1.執行t1,t2子線程I am reading at Thu Jan 31 13:26:55 2019I am listening music at Thu Jan 31 13:26:55 2019 2,進入等待5秒,在第三秒執行t1I am finish reading at Thu Jan 31 13:26:58 2019 3.再過倆秒執行t2和主線程I am finish listening music at Thu Jan 31 13:27:00 2019main threading ending at Thu Jan 31 13:27:00 2019...

三:總結

  在沒有join時,主線程會與子線程同時執行,但當子線程有了設置了join時,主線程會等待帶有join的子線程執行完畢再結束,如果一個進程的join設置再另一個線程開始之前,后者會在前者執行完畢再執行,如果是x.start+x.join+x1.start+x1.join。。。這種格式就成了穿行

?

  • 守護線程的概念
    • 只要主線程完成了,不管守護子線程是否完成,都要和主線程一起退出,整個Python會在所有的非守護線程結束后才會結束,即進程中沒有非守護線程存在的時候才結束

  • setDaemon方法實例理解

?

1 import threading 2 import time 3 def read(): 4 print("I am reading at %s"%time.ctime(),end="\n") 5 time.sleep(3) 6 print("I am finish reading at %s"%time.ctime(),end="\n") 7 def music(): 8 print("I am listening music at %s"%time.ctime()) 9 time.sleep(5) 10 print("I am finish listening music at %s"%time.ctime(),end="\n") 11 if __name__=="__main__": 12 t1=threading.Thread(target=read) 13 t2=threading.Thread(target=music) 14 t1.setDaemon(True) 15 t1.start() 16 t2.start() 17 print(r"main threading ending at %s..."%time.ctime()) View Code 一.設置t1為守護線程是看不到效果的,因為,主進程在所有非守護線程結束了才會結束 二:設置t2為守護線程1.首先執行t1,t2和主線程執行結果: I am reading at Fri Feb 1 20:28:58 2019I am listening music at Fri Feb 1 20:28:58 2019main threading ending at Fri Feb 1 20:28:58 2019...2.由于主線程會在所有非守護線程結束后才結束,所以非守護線程t1在3秒后結束,從而主線程結束,t2被迫結束執行結果:I am finish reading at Fri Feb 1 20:29:01 2019Process finished with exit code 0

?

# run(): 線程被cpu調度后自動執行線程對象的run方法 # start():啟動線程活動。 # isAlive(): 返回線程是否活動的。 # getName(): 返回線程名。 # setName(): 設置線程名。threading模塊提供的一些方法: # threading.currentThread(): 返回當前的線程變量。
      #<_MainThread(MainThread, started 14428)>#輸出的主線程名字MainThread # threading.enumerate(): 返回一個包含正在運行的線程的list。正在運行指線程啟動后、結束前,不包括啟動前和終止后的線程。
      #[<_MainThread(MainThread, started 13548)>, <Thread(Thread-1, started 10348)>, <Thread(Thread-2, started 18144)>]
      #從這里可以發現主線程實在所有線程結束后才結束
# threading.activeCount(): 返回正在運行的線程數量,與len(threading.enumerate())有相同的結果。

最后補充一下線程的類調用方法 https://www.cnblogs.com/yuanchenqi/articles/6248025.html 

?

    

?

    

?

?

         

?

轉載于:https://www.cnblogs.com/Mr-l/p/10337443.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python第九天(9-33)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。