from multiprocessing import Manager
- 使用Manager().list()初始化共享变量
result_list
with open(detourLogFile, 'r') as file:
reader = csv.reader(file)
detourLogData = list(reader)
result_list = Manager().list()
result_list.extend(detourLogData)
pool = Pool(processes=10)
for index, item in enumerate(detourLogData[1:]):
pool.apply_async(match_result, (result_list, index + 1, item,isFuzzyMatch))
pool.close()
pool.join()
-
match_result
方法里对result_list
的修改必须使用临时变量tmp
交换数据以触发__setitem__
方法,这样result_list
才能真正被修改
def match_result(result_list,index, item,isFuzzyMatch):
print("正在处理第"+str(index)+"条数据...")
currentData = detour_operation(item,isFuzzyMatch)
accounts, commands = conversion(currentData)
if accounts:
tmp = result_list[index]
tmp.append("\n".join(accounts))
tmp.append("\n".join(commands))
result_list[index] = tmp
网友评论