最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?multiprocessing?共享對象的示例代碼

 更新時間:2023年07月07日 09:08:10   作者:跡憶客  
在 Python 中使用 multiprocessing,一個新的進(jìn)程可以獨(dú)立運(yùn)行并擁有自己的內(nèi)存空間,下面通過示例代碼講解Python multiprocessing共享對象的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧

在 Python 中,共享內(nèi)存多處理由連接多個處理器組成,但這些處理器必須能夠直接訪問系統(tǒng)的主內(nèi)存。 這將允許所有連接的處理器訪問它們使用或創(chuàng)建的其他處理器數(shù)據(jù)。

在多進(jìn)程中使用 Python 共享內(nèi)存對象

在 Python 中使用 multiprocessing,一個新的進(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: []

我們使用上面的例子在兩個地方打印了全局?jǐn)?shù)組答案。

進(jìn)程 p 調(diào)用 square_numbers 函數(shù),以便在內(nèi)存空間中為進(jìn)程 p 更改數(shù)組元素。

主程序在進(jìn)程 p 完成后運(yùn)行,我們將在內(nèi)存空間中得到一個空數(shù)組作為答案。

Python 中的多處理提供了值對象和一個數(shù)組,用于在多個進(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)建了一個數(shù)組并將三個整數(shù)傳遞給它。 我們打印了一個平方值數(shù)組,然后是進(jìn)程 p 的平方值之和。

在此之后,我們再次為主程序打印一個平方值數(shù)組和平方值之和。

總結(jié)

可以通過多種方式來解釋使用 Python 的共享內(nèi)存多處理。 因此,在本文中,我們解釋了多進(jìn)程共享內(nèi)存概念,即一個對象如何放置在共享內(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新手入門之解釋器的安裝

    Python新手入門之解釋器的安裝

    相信有很多小伙伴還不會安裝Python解釋器,今天特地整理了本篇文章,文章有非常詳細(xì)的圖文示例,對不會安裝的小伙伴很有幫助,需要的朋友可以參考下
    2021-06-06
  • python實(shí)現(xiàn)手機(jī)銷售管理系統(tǒng)

    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)算分析

    這篇文章主要介紹了Python中的浮點(diǎn)數(shù)原理與運(yùn)算分析,結(jié)合實(shí)例形式分析了Python浮點(diǎn)數(shù)操作的常見錯誤,并簡單解釋了浮點(diǎn)數(shù)運(yùn)算的原理與比較運(yùn)算實(shí)現(xiàn)方法,需要的朋友可以參考下
    2017-10-10
  • Python中.py文件和.ipynb文件的區(qū)別詳解

    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í)例

    今天小編就為大家分享一篇python 經(jīng)典數(shù)字濾波實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python安裝讀取grib庫總結(jié)(推薦)

    python安裝讀取grib庫總結(jié)(推薦)

    這篇文章主要介紹了python安裝讀取grib庫總結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 用Python輸出一個楊輝三角的例子

    用Python輸出一個楊輝三角的例子

    這篇文章主要介紹了用Python和erlang輸出一個楊輝三角的例子,同時還提供了一個erlang版楊輝三角,需要的朋友可以參考下
    2014-06-06
  • linux之文件查找指定文件中包含關(guān)鍵字的行信息方式

    linux之文件查找指定文件中包含關(guān)鍵字的行信息方式

    這篇文章主要介紹了linux之文件查找指定文件中包含關(guān)鍵字的行信息方式,具有很好的參考價(jià)值,希望對大家有所幫助。
    2023-06-06
  • Python OpenCV簡單的繪圖函數(shù)使用教程

    Python OpenCV簡單的繪圖函數(shù)使用教程

    本文主要為大家介紹了OpenCV中一些簡單的繪圖函數(shù)的使用教程,文中的示例代碼講解詳細(xì),對我們了解OpenCV有一定的幫助,感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • 使用Pycharm+PyQt5彈出子窗口的程序代碼

    使用Pycharm+PyQt5彈出子窗口的程序代碼

    這篇文章主要介紹了使用Pycharm+PyQt5彈出子窗口的解決方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10

最新評論

蒙阴县| 逊克县| 奉贤区| 府谷县| 平江县| 仁化县| 浪卡子县| 喀喇| 新晃| 定远县| 栾城县| 中西区| 浦北县| 合作市| 衡阳市| 昌黎县| 黑水县| 博客| 枣庄市| 囊谦县| 安远县| 汕尾市| 深泽县| 温泉县| 浮梁县| 竹溪县| 宣武区| 大兴区| 独山县| 竹山县| 饶阳县| 山丹县| 丹巴县| 浑源县| 莱西市| 左权县| 广宗县| 绥中县| 上蔡县| 邵东县| 清原|