美文网首页
python 进程池共享变量

python 进程池共享变量

作者: 木火应 | 来源:发表于2022-07-05 09:57 被阅读0次
  • 引入Manager
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

相关文章

网友评论

      本文标题:python 进程池共享变量

      本文链接:https://www.haomeiwen.com/subject/biuqvrtx.html