Python?multiprocessing?共享對象的示例代碼
在 Python 中,共享內(nèi)存多處理由連接多個處理器組成,但這些處理器必須能夠直接訪問系統(tǒng)的主內(nèi)存。 這將允許所有連接的處理器訪問它們使用或創(chuàng)建的其他處理器數(shù)據(jù)。
在多進程中使用 Python 共享內(nèi)存對象
在 Python 中使用 multiprocessing,一個新的進程可以獨立運行并擁有自己的內(nèi)存空間。 通過查看下面的示例,讓我們詳細了解使用 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: []
我們使用上面的例子在兩個地方打印了全局數(shù)組答案。
進程 p 調(diào)用 square_numbers 函數(shù),以便在內(nèi)存空間中為進程 p 更改數(shù)組元素。
主程序在進程 p 完成后運行,我們將在內(nèi)存空間中得到一個空數(shù)組作為答案。
Python 中的多處理提供了值對象和一個數(shù)組,用于在多個進程之間共享數(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)建了一個數(shù)組并將三個整數(shù)傳遞給它。 我們打印了一個平方值數(shù)組,然后是進程 p 的平方值之和。
在此之后,我們再次為主程序打印一個平方值數(shù)組和平方值之和。
總結(jié)
可以通過多種方式來解釋使用 Python 的共享內(nèi)存多處理。 因此,在本文中,我們解釋了多進程共享內(nèi)存概念,即一個對象如何放置在共享內(nèi)存空間并獨立運行。
除此之外,我們還了解到 Python 允許進程在不同進程之間共享數(shù)據(jù)。
到此這篇關(guān)于Python multiprocessing 共享對象的文章就介紹到這了,更多相關(guān)Python multiprocessing 共享內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python利用 SVM 算法實現(xiàn)識別手寫數(shù)字
支持向量機 (Support Vector Machine, SVM) 是一種監(jiān)督學(xué)習(xí)技術(shù),它通過根據(jù)指定的類對訓(xùn)練數(shù)據(jù)進行最佳分離,從而在高維空間中構(gòu)建一個或一組超平面。本文將介紹通過SVM算法實現(xiàn)手寫數(shù)字的識別,需要的可以了解一下2021-12-12python目錄操作之python遍歷文件夾后將結(jié)果存儲為xml
需求是獲取服務(wù)器某個目錄下的某些類型的文件,考慮到服務(wù)器即有Linux、又有Windows,所以寫了一個Python小程序來完成這項工作,大家參考使用吧2014-01-01Python安裝Flask環(huán)境及簡單應(yīng)用示例
這篇文章主要介紹了Python安裝Flask環(huán)境及簡單應(yīng)用,結(jié)合實例形式分析了Flask框架的安裝、以及路由、重定向、cookie等相關(guān)操作實現(xiàn)方法,需要的朋友可以參考下2019-05-05Python實現(xiàn)向服務(wù)器請求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法示例
這篇文章主要介紹了Python實現(xiàn)向服務(wù)器請求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法,涉及Python文件傳輸及zip文件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06