目錄
- 一.Python 線程 threading 創(chuàng)建
- 二.Python 進(jìn)程 Process 創(chuàng)建
- 三.Python 進(jìn)程 Process 和線程 threading 區(qū)別
- 四.Python 進(jìn)程 Process 并行
- 五.Python 線程 threading 并發(fā)
- 六.猜你喜歡
一.Python 線程 threading 創(chuàng)建
對于 Python 線程相關(guān)的函數(shù)本文不再做詳細(xì)講解肾砂,如果想學(xué)習(xí)線程 threading 內(nèi)容請參考:Python 線程創(chuàng)建和參數(shù)傳遞
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個(gè)人博客地址): www.codersrc.com
@File:Python 進(jìn)程 Process 與線程 threading 區(qū)別.py
@Time:2021/05/07 08:00
@Motto:不積跬步無以至千里芒澜,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累淮阐!
"""
import threading
def study_info(*args,**kwargs):
print(args,kwargs)
def main():
# 信息列表
list_info = [{"name":"python 基礎(chǔ)","progress":"10%"},
{"name": "python 面向?qū)ο?, "progress": "20%"},
{"name": "python 爬蟲", "progress": "30%"},
{"name": "python pyqt5", "progress": "40%"},
{"name": "python 數(shù)據(jù)結(jié)構(gòu)", "progress": "50%"},]
# 創(chuàng)建線程
for i in range(5):
p = threading.Thread(target=study_info,args=(i,),kwargs=list_info[i])
# 啟動(dòng)線程
p.start()
if __name__ == "__main__":
main()
'''
輸出結(jié)果:
(0,) {'name': 'python 基礎(chǔ)', 'progress': '10%'}
(1,) {'name': 'python 面向?qū)ο?, 'progress': '20%'}
(2,) {'name': 'python 爬蟲', 'progress': '30%'}
(3,) {'name': 'python pyqt5', 'progress': '40%'}
(4,) {'name': 'python 數(shù)據(jù)結(jié)構(gòu)', 'progress': '50%'}
'''
二.Python 進(jìn)程 Process 創(chuàng)建
對于 Python 進(jìn)程相關(guān)的函數(shù)本文不再做詳細(xì)講解蔓腐,如果想學(xué)習(xí)進(jìn)程 Process 內(nèi)容請參考:Python 進(jìn)程 Process
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個(gè)人博客地址): www.codersrc.com
@File:Python 進(jìn)程 Process 與線程 threading 區(qū)別.py
@Time:2021/05/07 08:00
@Motto:不積跬步無以至千里矩乐,不積小流無以成江海,程序人生的精彩需要堅(jiān)持不懈地積累回论!
"""
from multiprocessing import Process
def study_info(*args,**kwargs):
print(args,kwargs)
def main():
# 信息列表
list_info = [{"name":"python 基礎(chǔ)","progress":"10%"},
{"name": "python 面向?qū)ο?, "progress": "20%"},
{"name": "python 爬蟲", "progress": "30%"},
{"name": "python pyqt5", "progress": "40%"},
{"name": "python 數(shù)據(jù)結(jié)構(gòu)", "progress": "50%"},]
# 創(chuàng)建進(jìn)程
for i in range(5):
p = Process(target=study_info,args=(i,),kwargs=list_info[i])
# 啟動(dòng)進(jìn)程
p.start()
if __name__ == "__main__":
main()
'''
輸出結(jié)果:
(0,) {'name': 'python 基礎(chǔ)', 'progress': '10%'}
(1,) {'name': 'python 面向?qū)ο?, 'progress': '20%'}
(2,) {'name': 'python 爬蟲', 'progress': '30%'}
(3,) {'name': 'python pyqt5', 'progress': '40%'}
(4,) {'name': 'python 數(shù)據(jù)結(jié)構(gòu)', 'progress': '50%'}
'''
三.Python 進(jìn)程 Process 和線程 threading 區(qū)別
Python 進(jìn)程 Process 和線程 threading 區(qū)別:
1.一個(gè)線程只能屬于一個(gè)進(jìn)程散罕,而一個(gè)進(jìn)程可以有多個(gè)線程,但至少有一個(gè)線程(線程是計(jì)算機(jī)的最小單位)傀蓉;
2.資源分配給進(jìn)程欧漱,同一進(jìn)程的所有線程共享該進(jìn)程的所有資源,進(jìn)程與進(jìn)程之間資源相互獨(dú)立葬燎,互不影響(類似深拷貝);
3.多進(jìn)程模式最大的優(yōu)點(diǎn)就是穩(wěn)定性高误甚,因?yàn)橐粋€(gè)子進(jìn)程崩潰了,不會影響主進(jìn)程和其他子進(jìn)程谱净,多進(jìn)程模式的缺點(diǎn)是在 Windows 下創(chuàng)建進(jìn)程開銷巨大靶草。另外,操作系統(tǒng)能同時(shí)運(yùn)行的進(jìn)程數(shù)也是有限的岳遥,在內(nèi)存和 CPU 的限制下,如果有幾千個(gè)進(jìn)程同時(shí)運(yùn)行裕寨,操作系統(tǒng)連調(diào)度都會成問題(進(jìn)程的創(chuàng)建比線程的創(chuàng)建更加占用計(jì)算機(jī)資源)浩蓉;
4.多線程模式致命的缺點(diǎn)就是任何一個(gè)線程掛掉都可能直接造成整個(gè)進(jìn)程崩潰,因?yàn)樗芯€程共享進(jìn)程的內(nèi)存宾袜;
**5.由于 GIL 鎖的緣故捻艳,Python 中線程實(shí)際上是并發(fā)運(yùn)行(即便有多個(gè) CPU** **,線程會在其中一個(gè) CPU** **來回切換庆猫,只占用一個(gè) CPU** **資源)认轨,而進(jìn)程才是真正的并行(同時(shí)執(zhí)行多個(gè)任務(wù),占用多個(gè) CPU** 資源)月培,下面關(guān)于并行和并發(fā)做一個(gè)簡單的了解嘁字;
四.Python 進(jìn)程 Process 并行
并行是指兩個(gè)或者多個(gè)事件在同一時(shí)刻發(fā)生,Python 中的進(jìn)程屬于并行杉畜,能充分利用計(jì)算機(jī)資源纪蜒,效率最高,**同時(shí)執(zhí)行多個(gè)任務(wù)此叠,占用多個(gè) CPU** **資源纯续;**
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-BMVew0rZ-1624930849839)(https://www.codersrc.com/wp-content/uploads/2021/05/c4ca4238a0b9238-1.png “Python 進(jìn)程 Process 與線程 threading 區(qū)別-猿說編程”)]
五.Python 線程 threading 并發(fā)
并發(fā)是指兩個(gè)或多個(gè)事件在同一時(shí)間間隔發(fā)生,Python 中的線程屬于并發(fā),不管計(jì)算機(jī)有多少個(gè) CPU 猬错,不管你開了多少個(gè)線程窗看,同一時(shí)間多個(gè)任務(wù)會在其中一個(gè) CPU 來回切換,只占用一個(gè) CPU 倦炒,效率并不高级解;
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-zu34UfQa-1624930849841)(https://www.codersrc.com/wp-content/uploads/2021/05/c81e728d9d4c2f6-2.png “Python 進(jìn)程 Process 與線程 threading 區(qū)別-猿說編程”)]
關(guān)于并行和并發(fā)我們留到后面 GIL 鎖在詳細(xì)講解;
六.猜你喜歡
- Python 條件推導(dǎo)式
- Python 列表推導(dǎo)式
- Python 字典推導(dǎo)式
- Python 函數(shù)聲明和調(diào)用
- Python 不定長參數(shù) *argc/**kargcs
- Python 匿名函數(shù) lambda
- Python return 邏輯判斷表達(dá)式
- Python 字符串/列表/元組/字典之間的相互轉(zhuǎn)換
- Python 局部變量和全局變量
- Python type 函數(shù)和 isinstance 函數(shù)區(qū)別
- Python is 和 == 區(qū)別
- Python 可變數(shù)據(jù)類型和不可變數(shù)據(jù)類型
- Python 淺拷貝和深拷貝
- Python 文件讀寫操作
- Python 異常處理
- Python 模塊 import
- Python __name__ == ‘__main__’詳細(xì)解釋
- Python 線程創(chuàng)建和傳參
- Python 線程互斥鎖 Lock
- Python 線程時(shí)間 Event
- Python 線程條件變量 Condition
- Python 線程定時(shí)器 Timer
- Python 線程信號量 Semaphore
- Python 線程障礙對象 Barrier
- Python 線程隊(duì)列 Queue – FIFO
- Python 線程隊(duì)列 LifoQueue – LIFO
- Python 線程優(yōu)先隊(duì)列 PriorityQueue
- Python 線程池 ThreadPoolExecutor(一)
- Python 線程池 ThreadPoolExecutor(二)
- Python 進(jìn)程 Process 模塊
- Python 進(jìn)程 Process 與線程 threading 區(qū)別
- Python 進(jìn)程間通信 Queue / Pipe
未經(jīng)允許不得轉(zhuǎn)載:猿說編程 ? Python 進(jìn)程 Process 與線程 threading 區(qū)別
本文由博客 - 猿說編程 猿說編程 發(fā)布华匾!