在进程之间分享dict [英] share dict between processes
问题描述
CldProc =进程(target = CloudRun)
CldProc.start()
我想知道我是否可以在CloudProc和我当前的主进程之间有一个共享字典?
编辑:
或者我正在考虑使用pickle将我的数据转储到进程的文件中,并将其加载回来,这需要我使用join()等待进程完成并退出。
第二个编辑
所以,我现在有我的dict被声明为mac_dict = {},然后我填写我的子进程,并希望在我的主进程中访问它。现在我刚刚尝试过:
>>> dict = dict()
>>> dict ['A'] = 1
>>> print dict
{'A':1}
那么Python如何知道dict )应该由经理打电话?
有没有我可以遵循的例子?
得到它,为了简化,我这样做: / p>
来自多进程导入进程,经理
def myf(myd):
myd [1 ] =你好!
def proc(d):
myf(d)
m = Manager()
locdict = m.dict()
locdict [2] =HI BUDDY!
p = Process(target = proc,args =(locdict,))
p.start()
p.join()
print locdict
I spawn a seperate process to handle my cloud services. I spawnb it like this:
CldProc = Process(target=CloudRun)
CldProc.start()
and am wondering if I can have a shared dictionary between that CloudProc and my current main process?
EDIT: Alternatively I am thinking to use pickle to dump my data into a file from the process and load it back, this requires me to use join() to wait for the process to complete and exit.
2nd EDIT So, I now have my dict declared like mac_dict={} and then I fill it in my subprocess and want to access it in my main process. Now I just tried this:
>>> dict = dict()
>>> dict['A'] = 1
>>> print dict
{'A': 1}
So how does Python know that dict() should be called from Managers? Is there any examples I can follow?
Got it, to simplify, I did it like this:
from multiprocessing import Process, Manager
def myf(myd):
myd[1] = "HELLO WORLD!"
def proc(d):
myf(d)
m=Manager()
locdict=m.dict()
locdict[2] = "HI BUDDY!"
p = Process(target=proc, args=(locdict,))
p.start()
p.join()
print locdict
这篇关于在进程之间分享dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!