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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Python系统学习第二十四课

發布時間:2023/12/20 windows 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python系统学习第二十四课 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多線程 VS 多進程

  • 程序:一堆代碼以文本形式存入一個文檔
  • 進程:程序運行的一個狀態
    • 包含地址空間,內存,數據棧
    • 每個進程有自己完全獨立的運行環境,多進程共享數據是一個問題
  • 線程
    • 一個進程的獨立運行片段,一個進程可以有多個線程
    • 輕量化的進程
    • 一個進程的多個線程間共享數據和上下文運行環境
    • 共享互斥問題
  • 全局解釋器鎖(GIL)
    • python代碼的執行是由python虛擬機進行控制
    • 在主循環中只能有一個控制線程在執行
  • python包
    • thread:有問題,不好用,python3版本改成了_thread
    • threading:通行的包
''' 利用time函數,生成兩個函數 順序調用 計算總的運行時間''' import timedef loop1():# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(4)print('End loop 1 at:', time.ctime())def loop2():# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(2)print('End loop 2 at:', time.ctime())def main():print("Starting at:", time.ctime())loop1()loop2()print("All done at:", time.ctime())if __name__ == '__main__':main() Starting at: Tue Jan 1 11:08:36 2019 Start loop 1 at : Tue Jan 1 11:08:36 2019 End loop 1 at: Tue Jan 1 11:08:40 2019 Start loop 2 at : Tue Jan 1 11:08:40 2019 End loop 2 at: Tue Jan 1 11:08:42 2019 All done at: Tue Jan 1 11:08:42 2019 ''' 利用time函數,生成兩個函數 順序調用 計算總的運行時間 使用多線程,縮短總時間,使用_thread''' import time import _thread as threaddef loop1():# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(4)print('End loop 1 at:', time.ctime())def loop2():# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(2)print('End loop 2 at:', time.ctime())def main():print("Starting at:", time.ctime())# 啟動多線程的意思是用多線程去執行某個函數# 啟動多線程函數為start_new_thead# 參數兩個,一個是需要運行的函數名,第二是函數的參數作為元祖使用,為空則使用空元祖# 注意:如果函數只有一個參數,需要參數后有一個逗號thread.start_new_thread(loop1, ())thread.start_new_thread(loop2, ())print("All done at:", time.ctime())if __name__ == '__main__':main()while True: #如果沒有這個循環就會產生線程開始工作但是沒有做完的事情發生,因為主線程在分線程結束之前就結束了time.sleep(1) Starting at: Tue Jan 1 11:15:30 2019 All done at: Tue Jan 1 11:15:30 2019 Start loop 1 at : Tue Jan 1 11:15:30 2019 Start loop 2 at : Tue Jan 1 11:15:30 2019 End loop 2 at: Tue Jan 1 11:15:32 2019 End loop 1 at: Tue Jan 1 11:15:34 2019 #多線程,傳參數 #利用time延時函數,生成兩個函數 # 利用多線程調用 # 計算總運行時間 # 練習帶參數的多線程啟動方法 import time # 導入多線程包并更名為thread import _thread as threaddef loop1(in1):# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 把參數打印出來print("我是參數 ",in1)# 睡眠多長時間,單位是秒time.sleep(4)print('End loop 1 at:', time.ctime())def loop2(in1, in2):# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 把參數in 和 in2打印出來,代表使用print("我是參數 " ,in1 , "和參數 ", in2)# 睡眠多長時間,單位是秒time.sleep(2)print('End loop 2 at:', time.ctime())def main():print("Starting at:", time.ctime())# 啟動多線程的意思是用多線程去執行某個函數# 啟動多線程函數為start_new_thead# 參數兩個,一個是需要運行的函數名,第二是函數的參數作為元祖使用,為空則使用空元祖# 注意:如果函數只有一個參數,需要參數后由一個逗號thread.start_new_thread(loop1,("王老大", ))thread.start_new_thread(loop2,("王大鵬", "王曉鵬"))print("All done at:", time.ctime())if __name__ == "__main__":main()# 一定要有while語句# 因為啟動多線程后本程序就作為主線程存在# 如果主線程執行完畢,則子線程可能也需要終止while True:time.sleep(10)
  • threading的使用
    • 直接使用threading.Thread生成Thread實例
      • 1.t = threading.Thread(target = xxx, args=(xxx, )) #target是函數名,args是參數
      • 2.t.start() #氣動多線程
      • 3.t.join():等待多線程執行完成
      • 守護線程,deamon
        • 如果在程序中將子線程設置成守護線程,則子線程會在主線程結束的時候自動退出
        • 一般認為,守護線程不重要或者不允許離開主線程獨立運行
        • 守護線程案例能否有效果跟環境相關
      • 線程常用屬性
        • threading.currentThread:返回當前線程變量
        • threading.enumerate:返回一個包含正在運行的線程的list,正在運行的線程指的是線程啟動后,結束前的線程
        • threading.ativeCount:返回正在運行的線程數量,效果跟len(threading.enumerate)相同
        • thr.setName:給線程設置名字
        • thr.getName:得到線程的名字
#利用time延時函數,生成兩個函數 # 利用多線程調用 # 計算總運行時間 # 練習帶參數的多線程啟動方法 import time # 導入多線程處理包 import threadingdef loop1(in1):# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 把參數打印出來print("我是參數 ",in1)# 睡眠多長時間,單位是秒time.sleep(4)print('End loop 1 at:', time.ctime())def loop2(in1, in2):# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 把參數in 和 in2打印出來,代表使用print("我是參數 " ,in1 , "和參數 ", in2)# 睡眠多長時間,單位是秒time.sleep(2)print('End loop 2 at:', time.ctime())def main():print("Starting at:", time.ctime())# 生成threading.Thread實例t1 = threading.Thread(target=loop1, args=("王老大",))t1.start()t2 = threading.Thread(target=loop2, args=("王大鵬", "王小鵬"))t2.start()print("All done at:", time.ctime())if __name__ == "__main__":main()# 一定要有while語句# 因為啟動多線程后本程序就作為主線程存在# 如果主線程執行完畢,則子線程可能也需要終止while True:time.sleep(10) #加入join之后的案例 #利用time延時函數,生成兩個函數 # 利用多線程調用 # 計算總運行時間 # 練習帶參數的多線程啟動方法 import time # 導入多線程處理包 import threadingdef loop1(in1):# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 把參數打印出來print("我是參數 ",in1)# 睡眠多長時間,單位是秒time.sleep(4)print('End loop 1 at:', time.ctime())def loop2(in1, in2):# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 把參數in 和 in2打印出來,代表使用print("我是參數 " ,in1 , "和參數 ", in2)# 睡眠多長時間,單位是秒time.sleep(2)print('End loop 2 at:', time.ctime())def main():print("Starting at:", time.ctime())# 生成threading.Thread實例t1 = threading.Thread(target=loop1, args=("王老大",))t1.start()t2 = threading.Thread(target=loop2, args=("王大鵬", "王小鵬"))t2.start()t1.join()t2.join()print("All done at:", time.ctime())if __name__ == "__main__":main()# 一定要有while語句# 因為啟動多線程后本程序就作為主線程存在# 如果主線程執行完畢,則子線程可能也需要終止while True:time.sleep(10) #非守護線程 #主線程結束了,但是分線程依然繼續執行 import time import threadingdef fun():print("Start fun")time.sleep(2)print("end fun")print("Main thread")t1 = threading.Thread(target=fun, args=() ) t1.start()time.sleep(1) print("Main thread end") #守護線程案例 import time import threadingdef fun():print("Start fun")time.sleep(2)print("end fun")print("Main thread")t1 = threading.Thread(target=fun, args=() ) # 社會守護線程的方法,必須在start之前設置,否則無效 t1.setDaemon(True) #t1.daemon = True #第二種設置方法 t1.start()time.sleep(1) print("Main thread end") import time import threadingdef loop1():# ctime 得到當前時間print('Start loop 1 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(6)print('End loop 1 at:', time.ctime())def loop2():# ctime 得到當前時間print('Start loop 2 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(1)print('End loop 2 at:', time.ctime())def loop3():# ctime 得到當前時間print('Start loop 3 at :', time.ctime())# 睡眠多長時間,單位是秒time.sleep(5)print('End loop 3 at:', time.ctime())def main():print("Starting at:", time.ctime())# 生成threading.Thread實例t1 = threading.Thread(target=loop1, args=( ))# setName是給每一個子線程設置一個名字t1.setName("THR_1")t1.start()t2 = threading.Thread(target=loop2, args=( ))t2.setName("THR_2")t2.start()t3 = threading.Thread(target=loop3, args=( ))t3.setName("THR_3")t3.start()# 預期3秒后,thread2已經自動結束,time.sleep(3)# enumerate 得到正在運行子線程,即子線程1和子線程3for thr in threading.enumerate():# getName能夠得到線程的名字print("正在運行的線程名字是: {0}".format(thr.getName()))print("正在運行的子線程數量為: {0}".format(threading.activeCount()))print("All done at:", time.ctime())if __name__ == "__main__":main()# 一定要有while語句# 因為啟動多線程后本程序就作為主線程存在# 如果主線程執行完畢,則子線程可能也需要終止while True:time.sleep(10) import threading import time# 1. 類需要繼承自threading.Thread class MyThread(threading.Thread):def __init__(self, arg):super(MyThread, self).__init__()self.arg = arg# 2 必須重寫run函數,run函數代表的是真正執行的功能def run(self):time.sleep(2)print("The args for this class is {0}".format(self.arg))for i in range(5):t = MyThread(i)t.start()t.join()print("Main thread is done!!!!!!!!") import threading from time import sleep, ctimeloop = [4,2]class ThreadFunc:def __init__(self, name):self.name = namedef loop(self, nloop, nsec):''':param nloop: loop函數的名稱:param nsec: 系統休眠時間:return:'''print('Start loop ', nloop, 'at ', ctime())sleep(nsec)print('Done loop ', nloop, ' at ', ctime())def main():print("Starting at: ", ctime())# ThreadFunc("loop").loop 跟一下兩個式子相等:# t = ThreadFunc("loop")# t.loop# 以下t1 和 t2的定義方式相等t = ThreadFunc("loop")t1 = threading.Thread( target = t.loop, args=("LOOP1", 4))# 下面這種寫法更西方人,工業化一點t2 = threading.Thread( target = ThreadFunc('loop').loop, args=("LOOP2", 2))# 常見錯誤寫法#t1 = threading.Thread(target=ThreadFunc('loop').loop(100,4))#t2 = threading.Thread(target=ThreadFunc('loop').loop(100,2))t1.start()t2.start()t1.join( )t2.join()print("ALL done at: ", ctime())if __name__ == '__main__':main()112

總結

以上是生活随笔為你收集整理的Python系统学习第二十四课的全部內容,希望文章能夠幫你解決所遇到的問題。

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