Python multiprocessing 共享对象

在 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 允许进程在不同进程之间共享数据。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迹忆客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值