这是我的问题的最小示例:
import concurrent.futures
from functools import partial
# Object class
class obj:
def __init__(self,tup):
self.tup = tup
# Function includes attributes in objects of class above
def new(fi,fdic):
fdic[fi].new = 'work_'+ str(fdic[fi].tup)
# Dictionary full of instances of obj above
dic = {'a':obj(1),
'b':obj(2),
'c':obj(3),
'd':obj(4),
'e':obj(5),
'f':obj(6),
}
partial_new = partial(new, fdic=dic)
现在我想对字典中的所有对象进行多处理(因为我实际上有太多)。下面的代码运行。但它不“工作”,因为我实际上需要 ProcessPool (我认为?因为我想并行处理事物)。
with concurrent.futures.ThreadPoolExecutor() as executor:
for _ in executor.map(partial_new, dic.keys()):
pass
print(dic['b'].new)
这个不运行:with concurrent.futures.ProcessPoolExecutor() as executor:
for _ in executor.map(partial_new, dic.keys()):
pass
print(dic['b'].new)
我的问题是:我如何使这项工作?我只需要使用该函数并行修改字典中的所有对象。稍后我将保存完整的字典,但我应用的函数不返回任何内容(如果这使事情变得更容易)。
最佳答案
问题是计算新值需要很长时间吗?
def get_new_value(dictionary_item):
key, value = dictionary_item
return key, 'work_' + str(value.tup)
with concurrent.futures.ProcessPoolExecutor() as executor:
for key, new_value in executor.map(get_new_value, dic.items()):
dic[key].new = new_value
您只能有一个线程修改 dic。但是您可以将键和值传递给线程,让线程返回键和新值,然后让原始线程完成更新字典的工作。您可能希望将块大小指定为
map
=== 编辑 ===正如所 promise 的,我的完整文件。
import concurrent.futures
# Object class
class obj:
def __init__(self, tup):
self.tup = tup
# Dictionary full of instances of obj above
dic = {'a': obj(1),
'b': obj(2),
'c': obj(3),
'd': obj(4),
'e': obj(5),
'f': obj(6),
}
def get_new_value(dictionary_item):
key, value = dictionary_item
return key, 'work_' + str(value.tup)
def go():
with concurrent.futures.ProcessPoolExecutor() as executor:
for key, new_value in executor.map(get_new_value, dic.items()):
dic[key].new = new_value
# Make sure it really worked!
for key, value in dic.items():
print(key, value.new)
if __name__ == '__main__':
go()
关于python - 如何并行循环以更改 Python 字典中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64877188/