迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

Python multiprocessing 共享对象

作者:迹忆客 最近更新:2023/06/13 浏览次数:

在 Python 中,共享内存多处理由连接多个处理器组成,但这些处理器必须能够直接访问系统的主内存。 这将允许所有连接的处理器访问它们使用或创建的其他处理器数据。


在多进程中使用 Python 共享内存对象

在 Python 中使用 multiprocessing,一个新的进程可以独立运行并拥有自己的内存空间。 通过查看下面的示例,让我们详细了解使用 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: []

我们使用上面的例子在两个地方打印了全局数组答案。

进程 p 调用 square_numbers 函数,以便在内存空间中为进程 p 更改数组元素。

主程序在进程 p 完成后运行,我们将在内存空间中得到一个空数组作为答案。

Python 中的多处理提供了值对象和一个数组,用于在多个进程之间共享数据。

示例代码:

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

在上面的示例中,我们创建了一个数组并将三个整数传递给它。 我们打印了一个平方值数组,然后是进程 p 的平方值之和。

在此之后,我们再次为主程序打印一个平方值数组和平方值之和。


总结

可以通过多种方式来解释使用 Python 的共享内存多处理。 因此,在本文中,我们解释了多进程共享内存概念,即一个对象如何放置在共享内存空间并独立运行。

除此之外,我们还了解到 Python 允许进程在不同进程之间共享数据。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas 多列合并

发布时间:2024/04/24 浏览次数:628 分类:Python

本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便