Python?multiprocessing?共享對象的示例代碼
在 Python 中,共享內(nèi)存多處理由連接多個(gè)處理器組成,但這些處理器必須能夠直接訪問系統(tǒng)的主內(nèi)存。 這將允許所有連接的處理器訪問它們使用或創(chuàng)建的其他處理器數(shù)據(jù)。
在多進(jìn)程中使用 Python 共享內(nèi)存對象
在 Python 中使用 multiprocessing,一個(gè)新的進(jìn)程可以獨(dú)立運(yùn)行并擁有自己的內(nèi)存空間。 通過查看下面的示例,讓我們詳細(xì)了解使用 Python 的共享對象多處理。
示例代碼:
import multiprocessing
#an empty array globally declared
answer = []
def square_numbers(mynumbers):
#for squaring array elements, a function has been used
global answer
#appending square numbers to a global array
for n in mynumbers:
answer.append(n * n)
#print a global array for generating an answer
print("Answer using first process: {}".format(answer))
if __name__ == "__main__":
#input array
mynumbers = [5,10,15]
#new process has been created
p = multiprocessing.Process(target=square_numbers, args=(mynumbers,))
#process begins here
p.start()
#wait unless a process is completed
p.join()
#print a global array for generating an answer
print("Answer using main program: {}".format(answer))輸出:
Answer using first process: [25, 100, 225]
Answer using main program: []
我們使用上面的例子在兩個(gè)地方打印了全局?jǐn)?shù)組答案。
進(jìn)程 p 調(diào)用 square_numbers 函數(shù),以便在內(nèi)存空間中為進(jìn)程 p 更改數(shù)組元素。
主程序在進(jìn)程 p 完成后運(yùn)行,我們將在內(nèi)存空間中得到一個(gè)空數(shù)組作為答案。
Python 中的多處理提供了值對象和一個(gè)數(shù)組,用于在多個(gè)進(jìn)程之間共享數(shù)據(jù)。
示例代碼:
import multiprocessing
def square_data(mydata, answer, square_sum):
#a function has been made for squaring of given data
#appending squares of mydata to the given array
for ix, n in enumerate(mydata):
answer[ix] = n * n
#sum the square values
square_sum.value = sum(answer)
#print array of squared values for process p
print("Answer in process p: {}".format(answer[:]))
# print the sum of squared values for process p
print("Sum of squares values in process p: {}".format(square_sum.value))
if __name__ == "__main__":
#here, we input the data
mydata = [1,2,3]
#an array has been created for the int data type for three integers
answer = multiprocessing.Array('i', 3)
#value has been created for int data type
square_sum = multiprocessing.Value('i')
#new process has been created
p = multiprocessing.Process(target=square_data, args=(mydata, answer, square_sum))
#process begins from here
p.start()
#wait unless the process is completed
p.join()
# print an array of squared values for the main program
print("Answer in main program: {}".format(answer[:]))
# print the sum of squared values for the main program
print("Sum of square values in main program: {}".format(square_sum.value))輸出:
Answer in process p: [1, 4, 9]
Sum of squares in process p: 14
Answer in main program: [1, 4, 9]
Sum of squares in main program: 14
在上面的示例中,我們創(chuàng)建了一個(gè)數(shù)組并將三個(gè)整數(shù)傳遞給它。 我們打印了一個(gè)平方值數(shù)組,然后是進(jìn)程 p 的平方值之和。
在此之后,我們再次為主程序打印一個(gè)平方值數(shù)組和平方值之和。
總結(jié)
可以通過多種方式來解釋使用 Python 的共享內(nèi)存多處理。 因此,在本文中,我們解釋了多進(jìn)程共享內(nèi)存概念,即一個(gè)對象如何放置在共享內(nèi)存空間并獨(dú)立運(yùn)行。
除此之外,我們還了解到 Python 允許進(jìn)程在不同進(jìn)程之間共享數(shù)據(jù)。
到此這篇關(guān)于Python multiprocessing 共享對象的文章就介紹到這了,更多相關(guān)Python multiprocessing 共享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)手機(jī)銷售管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)手機(jī)銷售管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析
這篇文章主要介紹了Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析,結(jié)合實(shí)例形式分析了Python浮點(diǎn)數(shù)操作的常見錯(cuò)誤,并簡單解釋了浮點(diǎn)數(shù)運(yùn)算的原理與比較運(yùn)算實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
Python中.py文件和.ipynb文件的區(qū)別詳解
Python開發(fā)者常用的兩種文件格式.py和.ipynb各有特點(diǎn),本教程將通過對比分析、代碼示例和場景說明,幫助開發(fā)者全面理解二者的區(qū)別與聯(lián)系,需要的朋友可以參考下2025-04-04
python 經(jīng)典數(shù)字濾波實(shí)例
今天小編就為大家分享一篇python 經(jīng)典數(shù)字濾波實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
linux之文件查找指定文件中包含關(guān)鍵字的行信息方式
這篇文章主要介紹了linux之文件查找指定文件中包含關(guān)鍵字的行信息方式,具有很好的參考價(jià)值,希望對大家有所幫助。2023-06-06

