總結(jié)python多進(jìn)程multiprocessing的相關(guān)知識
multiprocessing多進(jìn)程
概念

創(chuàng)建多進(jìn)程基本流程

創(chuàng)建進(jìn)程對象

啟動進(jìn)程 回收進(jìn)程

代碼:
import multiprocessing as mp
from time import sleep
# 進(jìn)程執(zhí)行函數(shù)
def fun():
print("開始一個進(jìn)程")
sleep(3)
print("進(jìn)程結(jié)束")
# 創(chuàng)建進(jìn)程對象
p = mp.Process(target = fun)
p.start() # 啟動進(jìn)程
p.join() # 回收進(jìn)程
運(yùn)行結(jié)果:
開始一個進(jìn)程
進(jìn)程結(jié)束
Process finished with exit code 0
1 、父子進(jìn)程是并行執(zhí)行的 子進(jìn)程執(zhí)行函數(shù) 父進(jìn)程執(zhí)行除子進(jìn)程外內(nèi)容:
import multiprocessing as mp
from time import sleep
# 進(jìn)程執(zhí)行函數(shù)
def fun():
print("開始一個進(jìn)程")
sleep(3)
print("進(jìn)程結(jié)束")
# 創(chuàng)建進(jìn)程對象
p = mp.Process(target = fun) # 把fun函數(shù)作為獨(dú)立子進(jìn)程 其它函數(shù)由進(jìn)程來執(zhí)行
p.start() # 啟動進(jìn)程
sleep(2)
print("父進(jìn)程執(zhí)行內(nèi)容")
p.join() # 回收進(jìn)程
print("===============")
"""
pid = os.fork
if pid == 0
fun()
os._exit(0)
else:
os.wait()
"""
運(yùn)行結(jié)果:
開始一個進(jìn)程
父進(jìn)程執(zhí)行內(nèi)容
進(jìn)程結(jié)束
===============
2、子進(jìn)程不能改變父進(jìn)程中變量的值
代碼:
import multiprocessing as mp
from time import sleep
a = 1
# 進(jìn)程執(zhí)行函數(shù)
def fun():
print("開始一個進(jìn)程")
sleep(3)
global a
print("a=", a)
a = 1000
print("a=", a)
print("進(jìn)程結(jié)束")
# 創(chuàng)建進(jìn)程對象
p = mp.Process(target = fun) # 把fun函數(shù)作為獨(dú)立子進(jìn)程 其它函數(shù)由進(jìn)程來執(zhí)行
p.start() # 啟動進(jìn)程
sleep(2)
print("父進(jìn)程執(zhí)行內(nèi)容")
p.join() # 回收進(jìn)程
print("===============")
print("a=", a)
運(yùn)行結(jié)果:
開始一個進(jìn)程
父進(jìn)程執(zhí)行內(nèi)容
a= 1
a= 1000
進(jìn)程結(jié)束
a= 1
創(chuàng)建多個進(jìn)程
代碼:
"""
創(chuàng)建多個進(jìn)程
"""
from multiprocessing import Process
import os
from time import sleep
def fun1():
sleep(2)
print(os.getppid(), '--', os.getpid(), "吃飯")
def fun2():
sleep(3)
print(os.getppid(), '--', os.getpid(), "睡覺")
def fun3():
sleep(4)
print(os.getppid(), '--', os.getpid(), "學(xué)習(xí)")
jobs =[]
for th in [fun1, fun2, fun3]:
p = Process(target = th)
jobs.append(p)
p.start()
for i in jobs:
i.join()
運(yùn)行結(jié)果:
46013 – 46022 吃飯
46013 – 46023 睡覺
46013 – 46024 學(xué)習(xí)
含有參數(shù)的進(jìn)程函數(shù)
代碼:
from multiprocessing import Process
from time import sleep
# 含有參數(shù)的進(jìn)程函數(shù)
def worker(sec, name):
for i in range(3):
sleep(sec)
print("I'm %s"%name)
print("I'm working...")
# p = Process(target = worker, args = (2, "Tom"))
p = Process(target = worker, kwargs = {'name':'tom', 'sec': 2})
p.start()
p.join()
運(yùn)行結(jié)果:
I'm tom
I'm working…
I'm tom
I'm working…
I'm tom
I'm working…
案例練習(xí)

代碼:
from multiprocessing import Process
import os
filename = './dace.jpg'
size = os.path.getsize(filename)
# 復(fù)制上半部分
def up():
fr = open(filename, 'rb')
fw = open('bot,jpg', 'wb')
n = size//2
fw.write(fr.read(n))
fw.close()
fr.close()
# 復(fù)制下半部分
def down():
fr = open(filename, 'rb')
fw = open('bot,jpg', 'wb')
fr.seek(size//2.0)
fw.write(fr.read())
fw.close()
fr.close()
p = Process(target = up)
q = Process(target = down)
p.start()
q.start()
p.join()
q.join()
到此這篇關(guān)于總結(jié)python多進(jìn)程multiprocessing的相關(guān)知識的文章就介紹到這了,更多相關(guān)python multiprocessing多進(jìn)程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ubuntu16.04 安裝多個python版本的問題及解決方法
Ubuntu16.04自帶python2.7與python3.5,Ubuntu 官方 apt 庫中還未收錄 python 3.8,因此添加 deadsnakes PPA 源安裝python3.8,否則會出現(xiàn)報錯,接下來通過本文給大家介紹Ubuntu16.04 安裝python的問題,一起看看吧2021-09-09
python標(biāo)準(zhǔn)庫中inspect模塊的簡單說明
這篇文章主要介紹了python標(biāo)準(zhǔn)庫中inspect模塊的簡單介紹,inspect模塊提供了幾個有用的函數(shù)來幫助獲取有關(guān)活動對象的信息,例如模塊,類,方法,函數(shù),回溯,框架對象和代碼對象,需要的朋友可以參考下2023-08-08
Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例
本篇文章主要介紹了Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
為Python的web框架編寫MVC配置來使其運(yùn)行的教程
這篇文章主要介紹了為Python的web框架編寫MVC配置來使其運(yùn)行的教程,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
Python-torch?之torch.clamp()?函數(shù)解析
torch.clamp()函數(shù)用于對輸入張量進(jìn)行截斷操作,將張量中的每個元素限制在指定的范圍內(nèi),這篇文章主要介紹了Python torch之torch.clamp()函數(shù),需要的朋友可以參考下2023-05-05
python腳本使用阿里云slb對惡意攻擊進(jìn)行封堵的實(shí)現(xiàn)
這篇文章主要介紹了python腳本使用阿里云slb對惡意攻擊進(jìn)行封堵的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Python環(huán)境下安裝使用異步任務(wù)隊(duì)列包Celery的基礎(chǔ)教程
這篇文章主要介紹了Python環(huán)境下安裝使用異步任務(wù)隊(duì)列包Celery的基礎(chǔ)教程,Celery的分布式任務(wù)管理適合用于服務(wù)器集群的管理和維護(hù),需要的朋友可以參考下2016-05-05

