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

歡迎訪問 生活随笔!

生活随笔

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

python

Python 进程 Process 与线程 threading 区别 - Python零基础入门教程

發布時間:2024/9/27 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 进程 Process 与线程 threading 区别 - Python零基础入门教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 一.Python 線程 threading 創建
  • 二.Python 進程 Process 創建
  • 三.Python 進程 Process 和線程 threading 區別
  • 四.Python 進程 Process 并行
  • 五.Python 線程 threading 并發
  • 六.猜你喜歡

一.Python 線程 threading 創建

對于 Python 線程相關的函數本文不再做詳細講解,如果想學習線程 threading 內容請參考:Python 線程創建和參數傳遞

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python 進程 Process 與線程 threading 區別.py @Time:2021/05/07 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""import threadingdef study_info(*args,**kwargs):print(args,kwargs)def main():# 信息列表list_info = [{"name":"python 基礎","progress":"10%"},{"name": "python 面向對象", "progress": "20%"},{"name": "python 爬蟲", "progress": "30%"},{"name": "python pyqt5", "progress": "40%"},{"name": "python 數據結構", "progress": "50%"},]# 創建線程for i in range(5):p = threading.Thread(target=study_info,args=(i,),kwargs=list_info[i])# 啟動線程p.start()if __name__ == "__main__":main()''' 輸出結果:(0,) {'name': 'python 基礎', 'progress': '10%'} (1,) {'name': 'python 面向對象', 'progress': '20%'} (2,) {'name': 'python 爬蟲', 'progress': '30%'} (3,) {'name': 'python pyqt5', 'progress': '40%'} (4,) {'name': 'python 數據結構', 'progress': '50%'} '''

二.Python 進程 Process 創建

對于 Python 進程相關的函數本文不再做詳細講解,如果想學習進程 Process 內容請參考:Python 進程 Process

# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿說編程 @Blog(個人博客地址): www.codersrc.com @File:Python 進程 Process 與線程 threading 區別.py @Time:2021/05/07 08:00 @Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!"""from multiprocessing import Processdef study_info(*args,**kwargs):print(args,kwargs)def main():# 信息列表list_info = [{"name":"python 基礎","progress":"10%"},{"name": "python 面向對象", "progress": "20%"},{"name": "python 爬蟲", "progress": "30%"},{"name": "python pyqt5", "progress": "40%"},{"name": "python 數據結構", "progress": "50%"},]# 創建進程for i in range(5):p = Process(target=study_info,args=(i,),kwargs=list_info[i])# 啟動進程p.start()if __name__ == "__main__":main()''' 輸出結果:(0,) {'name': 'python 基礎', 'progress': '10%'} (1,) {'name': 'python 面向對象', 'progress': '20%'} (2,) {'name': 'python 爬蟲', 'progress': '30%'} (3,) {'name': 'python pyqt5', 'progress': '40%'} (4,) {'name': 'python 數據結構', 'progress': '50%'} '''

三.Python 進程 Process 和線程 threading 區別

Python 進程 Process 和線程 threading 區別:

1.一個線程只能屬于一個進程,而一個進程可以有多個線程,但至少有一個線程(線程是計算機的最小單位)

2.資源分配給進程,同一進程的所有線程共享該進程的所有資源,進程與進程之間資源相互獨立,互不影響(類似深拷貝);

3.多進程模式最大的優點就是穩定性高,因為一個子進程崩潰了,不會影響主進程和其他子進程,多進程模式的缺點是在 Windows 下創建進程開銷巨大。另外,操作系統能同時運行的進程數也是有限的,在內存和 CPU 的限制下,如果有幾千個進程同時運行,操作系統連調度都會成問題**(進程的創建比線程的創建更加占用計算機資源)**;

4.多線程模式致命的缺點就是任何一個線程掛掉都可能直接造成整個進程崩潰,因為所有線程共享進程的內存;

**5.由于 GIL 鎖的緣故,Python 中線程實際上是并發運行(即便有多個 CPU** **,線程會在其中一個 CPU** **來回切換,只占用一個 CPU** **資源),而進程才是真正的并行(同時執行多個任務,占用多個 CPU** 資源),下面關于并行和并發做一個簡單的了解;

四.Python 進程 Process 并行

并行是指兩個或者多個事件在同一時刻發生,Python 中的進程屬于并行能充分利用計算機資源效率最高,**同時執行多個任務,占用多個 CPU** **資源;**

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-BMVew0rZ-1624930849839)(https://www.codersrc.com/wp-content/uploads/2021/05/c4ca4238a0b9238-1.png “Python 進程 Process 與線程 threading 區別-猿說編程”)]

五.Python 線程 threading 并發

并發是指兩個或多個事件在同一時間間隔發生,Python 中的線程屬于并發不管計算機有多少個 CPU ,不管你開了多少個線程,同一時間多個任務會在其中一個 CPU 來回切換,只占用一個 CPU ,效率并不高;

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zu34UfQa-1624930849841)(https://www.codersrc.com/wp-content/uploads/2021/05/c81e728d9d4c2f6-2.png “Python 進程 Process 與線程 threading 區別-猿說編程”)]

關于并行和并發我們留到后面 GIL 鎖在詳細講解;

六.猜你喜歡

  • Python 條件推導式
  • Python 列表推導式
  • Python 字典推導式
  • Python 函數聲明和調用
  • Python 不定長參數 *argc/**kargcs
  • Python 匿名函數 lambda
  • Python return 邏輯判斷表達式
  • Python 字符串/列表/元組/字典之間的相互轉換
  • Python 局部變量和全局變量
  • Python type 函數和 isinstance 函數區別
  • Python is 和 == 區別
  • Python 可變數據類型和不可變數據類型
  • Python 淺拷貝和深拷貝
  • Python 文件讀寫操作
  • Python 異常處理
  • Python 模塊 import
  • Python __name__ == ‘__main__’詳細解釋
  • Python 線程創建和傳參
  • Python 線程互斥鎖 Lock
  • Python 線程時間 Event
  • Python 線程條件變量 Condition
  • Python 線程定時器 Timer
  • Python 線程信號量 Semaphore
  • Python 線程障礙對象 Barrier
  • Python 線程隊列 Queue – FIFO
  • Python 線程隊列 LifoQueue – LIFO
  • Python 線程優先隊列 PriorityQueue
  • Python 線程池 ThreadPoolExecutor(一)
  • Python 線程池 ThreadPoolExecutor(二)
  • Python 進程 Process 模塊
  • Python 進程 Process 與線程 threading 區別
  • Python 進程間通信 Queue / Pipe
  • 未經允許不得轉載:猿說編程 ? Python 進程 Process 與線程 threading 區別

    總結

    以上是生活随笔為你收集整理的Python 进程 Process 与线程 threading 区别 - Python零基础入门教程的全部內容,希望文章能夠幫你解決所遇到的問題。

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