一个类中的Python多处理共享dict [英] Python multiprocessing shared dict inside a class

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

问题描述

我想了解多处理和管理器为了共享内存而工作



我有一个类,在类上创建一个字典。 init
我想使用多进程来调用填充字典的类函数(每个进程添加一个不同的键)。

  import multiprocessing as mp 
from multiprocessing import Process,Manager

class num:
def __init __(self):
manager = Manager()
d = manager.dict()

#进程的安装列表
processes = [mp.Process(target = self.f,args =(d,i))for i in range (5)]

#Run进程中的p

p.start()

#完成已完成的进程
对于p进程:
p.join()

打印d

def f(self,d,i):
d [str(i )] = []
d [str(i)]。append(i)


如果__name__ =='__main__':
test = num()

result: p>

  {'1':[],'0':[],'3':[],'2' ,'4':[]} 

f()中的列表需要共享吗?如何和为什么?

解决方案

您需要更改函数 f from:

  d ['i'] = [i] 
/ pre>

  d [str(i) ] = i 

,以便您的进程不会覆盖共享dict中的彼此的条目。之后,它适用于我(在Python 2.7.3中),打印出来

  {'1':1,' 0':0,'3':3,'2':2,'4':4} 

(另外,您发布的代码缺少一个导入多重处理作为mp



更新:如果您只想将共享dict中的值作为列表,那么这是一个简单的更改,例如

  d [str(i)] = [i] 

如果您希望每个列表共享所有的进程,那么你可能需要使用 manager.list()在主进程中创建这些列表,并将它们传递给所有的子进程,例如: p>

  count = 5 
lists =我在范围(count)中的[manager.list()]
我的范围(计数):
d [i] =列表[i]
processes = [mp.Process(target = self.f,args =(d,i,lists)) (计数)]

[...] p>

  def f(self,d,i,lists):
for range(i):#只是一个例子显示
列表[j] .append(i)#列表在进程之间共享

我也尝试直接嵌套托管的托管列表,但由于某种原因,这没有工作,子进程无法更新列表。如图所示分开通过它们似乎工作,例如我可以得到每个子进程来更新多个列表:

  {0:[1,2,3,4],1: 2,3,4],2:[3,4],3:[4],4:[]} 


I want to understand how multiprocessing and manager works in order to shared memory

I have a class with a dictionary created on class.init I want to use multiprocessing in order to call a class function which fill the dictionary (every process add a different key).

import multiprocessing as mp
from multiprocessing import Process, Manager

class num:
    def __init__(self):
        manager = Manager()
        d = manager.dict()

        # Setup list of processes
        processes = [mp.Process(target=self.f, args=(d,i)) for i in range(5)]

        #Run processes 
        for p in processes:
            p.start()

        #Exit the completed processes
        for p in processes:
            p.join()

        print d

    def f(self,d,i):
        d[str(i)] = []
        d[str(i)].append(i)


if __name__ == '__main__':      
    test = num()

result:

{'1': [], '0': [], '3': [], '2': [], '4': []}

The list inside f() need to be shared too? How and why?

解决方案

You need to change the line in the function f from:

d['i'] = [i]

to something like

d[str(i)] = i

so that your processes don't overwrite one another's entries in the shared dict. After that, it works fine for me (in Python 2.7.3), printing out

{'1': 1, '0': 0, '3': 3, '2': 2, '4': 4}

(Also, the code you posted is missing an import multiprocessing as mp)

Update: if you just want the values in the shared dict to be lists, then that's a simple change, e.g.

d[str(i)] = [i]

If you want each of the lists to be shared across all the processes, then you probably need to create those lists in the main process, using manager.list(), and pass those to all the subprocesses, for example:

    count = 5
    lists = [manager.list() for i in range(count)]
    for i in range(count):
        d[i] = lists[i]
    processes = [mp.Process(target=self.f, args=(d,i, lists)) for i in range(count)]

[...]

def f(self,d,i, lists):
    for j in range(i):       # just an example to show 
        lists[j].append(i)   # that the lists are shared between processes

I also tried directly nesting the managed lists inside the managed dict, but for some reason this didn't work, and the subprocesses could not update the lists. Passing them separately as shown seems to work, e.g. I can get each subprocess to update multiple lists:

{0: [1, 2, 3, 4], 1: [2, 3, 4], 2: [3, 4], 3: [4], 4: []}

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

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