我需要你的多处理帮助来更新全局字典数据。 我使用了两个函数,第一个函数是在 for 循环中进行多处理。 第二个功能是更新全局字典数据。 多重处理后,所有数据都没有更新。
这是我的示例代码:
import concurrent.futures
from multiprocessing import Process
from multiprocessing import Manager
list_key = ['A','B','C','D','E','F','G','H','I','K']
# first function. increase the value in Dict_value
def first(x,y):
print('Call Increase Value Function')
print(x)
print(y[x])
y[x] += 1
print(y[x])
print('Finished Increase Value')
# second function. square the value in the Dict_value
def second(x,y):
print('Call sqrt_dict_value function')
print(y)
for i in x:
y[i] = y[i]* y[i]
print(y[i])
print('Finished sqrt_dict_value')
def main():
# declare the list and dict
with Manager() as manager :
dict_value = manager.dict()
list_key = manager.list()
list_key = ['A','B','C','D','E','F','G','H','I','K']
dict_value = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'K':9}
# multiprocessing for first function.
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
process = executor.submit(first,list_key,dict_value)
# square the value in the dict after increase value in the first function.
second(list_key,dict_value)
if __name__ == '__main__':
main()
我的预期结果是: 1个 4个 9 16 25 36 49 64 81 100
终于,我找到了我帖子的答案。 但我不喜欢这种方式。因为我们需要在我的子函数中返回更多的值,并添加 For 循环以重新收集数据。 我仍在寻找另一种方法,例如 multiprocessing.Manager() 在程序执行多进程期间保存所有数据。 这是我的 main() 新代码:
def main():
time_start = time.time()
# declare the list and dict
with Manager() as manager :
# dict_value = manager.dict()
# list_key = manager.list()
# list_key = ['A','B','C','D','E','F','G','H','I','K']
# dict_value = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'K':9}
list_key = []
dict_value = {}
for i in range(1000):
list_key.append(i)
dict_value[i]=i
# multiprocessing for first function.
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
for i in list_key:
process = executor.submit(first,i,dict_value)
process_output.append(process)
for result in process_output:
if result._result[0] in dict_value:
print(result._result[0])
print(result._result[1])
dict_value[result._result[0]]= result._result[1]
else:
dict_value.append(result._result)
print(dict_value)
print('Finished Increase Value')
# square the value in the dict after increase value in the first function.
second(list_key,dict_value)
time_end = time.time()
print(time_end-time_start)
print('Finished')