多处理-共享阵列 [英] Multiprocessing - Shared Array

查看:108
本文介绍了多处理-共享阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试在python中实现多处理,我希望有一个4-5个进程的池并行运行一个方法.这样做的目的是总共运行1000个Monte模拟(每个进程250-200个模拟),而不是运行1000.我希望每个进程通过在完成处理后立即获取对它的锁定,从而将其写入公用共享数组.一个模拟的结果,写入结果并释放锁.因此,这应该是一个三步过程:

So I'm trying to implement multiprocessing in python where I wish to have a Pool of 4-5 processes running a method in parallel. The purpose of this is to run a total of thousand Monte simulations (250-200 simulations per process) instead of running 1000. I want each process to write to a common shared array by acquiring a lock on it as soon as its done processing the result for one simulation, writing the result and releasing the lock. So it should be a three step process :

  1. 获取锁定
  2. 写结果
  3. 为其他等待写入数组的进程释放锁定.

每次我将数组传递给进程时,每个进程都会创建该数组的副本,因为我想要一个通用数组,所以我不需要该数组的副本.任何人都可以通过提供示例代码来帮助我吗?

Everytime I pass the array to the processes each process creates a copy of that array which I donot want as I want a common array. Can anyone help me with this by providing sample code?

推荐答案

未经测试,但类似的方法应该可以工作. 数组和锁在进程之间共享.

Not tested, but something like that should work. The array and lock are shared between processes.

from multiprocessing import Process, Array, Lock

def f(array, lock, n): #n is the dedicated location in the array
    lock.acquire()
    array[n]=-array[n]
    lock.release()

if __name__ == '__main__':
    size=100
    arr=Array('i', [3,-7])
    lock=Lock()
    p = Process(target=f, args=(arr,lock,0))
    q = Process(target=f, args=(arr,lock,1))
    p.start()
    q.start()
    q.join()
    p.join()

    print(arr[:])

此处的文档 https://docs.python.org/3.5/library/multiprocessing.html 有很多示例开头

the documentation here https://docs.python.org/3.5/library/multiprocessing.html has plenty of examples to start with

这篇关于多处理-共享阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆