我正在创建一个这样的程序。
from __future__ import print_function
import multiprocessing
dict1 = dict( (k,v) for (k,v) in zip(range(0,11),range(50,61)))
dict2={}
dict3={}
def fun1(dct):
#How can I process `dct` as `multiprocessing.Pool` would act like a loop sending chunks of iterable?
#I can do:
# for i in dct:
# dict2.update({dct[i]:i})
# but `multiprocessing.Pool` will do the looping part automatically. In this case what should be done to index `dct`?
#dict2.update({dct[i]:i})
return dict2
if __name__ == '__main__':
p=multiprocessing.Pool(2)
dict3=p.map(fun1,dict1)
p.close()
p.join()
print(dict3) #write in file我希望修改函数fun1中的全局变量fun1,并将更新的全局变量返回给要打印的主函数(将其写入文件)。然而,在此之前,我会得到错误的TypeError: 'int' object is unsubscriptable。我怎么才能让这个起作用?
我读到了一些关于如何使用关键字global修改全局变量的问题,但是每次将global dict2放在fun1()中都会重置变量。
更新:
我如何处理像dct一样的dct[0],因为multiprocessing.Pool的作用就像一个发送块可迭代的循环一样?
发布于 2017-08-23 14:18:39
这使您获得了dict2的最终版本,但我不确定multiprocessing是否为您提供了性能方面的任何东西。
警告: Python3警报
import multiprocessing
dict1 = dict( (k,v) for (k,v) in zip(range(0,11),range(50,61)))
dict2={}
def fun1(dct):
key, value, dict2 = dct #unpack tuple
# This code would not update dict2
# dict2.update({value:key})
# return the value and key reversed. I assume this is what you are after.
return {value:key}
if __name__ == '__main__':
p=multiprocessing.Pool(2)
# pass the tuple of (key,value,dict2) into each call of fun1
dict3=p.map(fun1,((key,value,dict2) for key, value in dict1.items()))
p.close()
p.join()
print(dict1) # original dict
print(dict2) # remains empty
print(dict3) # this is a list of the results
for d in dict3:
dict2.update(d)
# Now dict2 is populated
print(dict2)输出:
{0: 50, 1: 51, 2: 52, 3: 53, 4: 54, 5: 55, 6: 56, 7: 57, 8: 58, 9: 59, 10: 60}
{}
[{50: 0}, {51: 1}, {52: 2}, {53: 3}, {54: 4}, {55: 5}, {56: 6}, {57: 7}, {58: 8}, {59: 9}, {60: 10}]
{50: 0, 51: 1, 52: 2, 53: 3, 54: 4, 55: 5, 56: 6, 57: 7, 58: 8, 59: 9, 60: 10}https://stackoverflow.com/questions/45840915
复制相似问题