問題描述
我想在我的進程之間共享一個字典,如下所示:
I want to share a dictionary between my processes as follows:
def f(y,x):
y[x]=[x*x]
if __name__ == '__main__':
pool = Pool(processes=4)
inputs = range(10)
y={}
result = pool.map(f,y,inputs)
y 返回 {}.我怎樣才能讓它發(fā)揮作用?
The y returns {}. How can I make it work?
謝謝,
推薦答案
這看起來你正在使用 multiprocessing
模塊.你沒有說,這是一個重要的信息.
This looks like you are using the multiprocessing
module. You didn't say, and that's an important bit of information.
multiprocessing.Pool()
實例上的 .map()
函數(shù)有兩個參數(shù):一個函數(shù)和一個序列.將使用序列中的連續(xù)值調(diào)用該函數(shù).
The .map()
function on a multiprocessing.Pool()
instance takes two arguments: a function, and a sequence. The function will be called with successive values from the sequence.
您不能在像 dict
這樣的可變變量中收集值(在示例中,它是參數(shù) y
),因為您的代碼將在多個不同的進程中運行.將值寫入另一個進程中的 dict
不會將該值發(fā)送回原始進程.但是如果你使用 Pool.map()
其他進程將返回每個函數(shù)調(diào)用的結果,返回到第一個進程.然后你可以收集這些值來構建一個 dict
.
You can't collect values in a mutable like a dict
(in the example, it's argument y
) because your code will be running in multiple different processes. Writing a value to a dict
in another process doesn't send that value back to the original process. But if you use Pool.map()
the other processes will return the result from each function call, back to the first process. Then you can collect the values to build a dict
.
示例代碼:
import multiprocessing as mp
def f(x):
return (x, x*x)
if __name__ == '__main__':
pool = mp.Pool()
inputs = range(10)
result = dict(pool.map(f, inputs))
result
設置為:{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8:64, 9:81}
讓我們改變它,而不是計算 x*x
它將提高 x
到某個冪,并且將提供該冪.讓我們讓它接受一個字符串鍵參數(shù).這意味著 f()
需要接受一個元組參數(shù),其中元組將是 (key, x, p)
并且它將計算 x**p
.
Let's change it so that instead of computing x*x
it will raise x
to some power, and the power will be provided. And let's make it take a string key argument. This means that f()
needs to take a tuple argument, where the tuple will be (key, x, p)
and it will compute x**p
.
import multiprocessing as mp
def f(tup):
key, x, p = tup # unpack tuple into variables
return (key, x**p)
if __name__ == '__main__':
pool = mp.Pool()
inputs = range(10)
inputs = [("1**1", 1, 1), ("2**2", 2, 2), ("2**3", 2, 3), ("3**3", 3, 3)]
result = dict(pool.map(f, inputs))
如果您有多個序列,并且需要將它們連接在一起以形成上述的單個序列,請考慮使用 zip()
或 itertools.product
.
If you have several sequences and you need to join them together to make a single sequence for the above, look into using zip()
or perhaps itertools.product
.
這篇關于Python 在并行進程之間共享字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!