首页 > 系统相关 >Python 多进程(一)简单场景

Python 多进程(一)简单场景

时间:2022-11-21 22:47:47浏览次数:31  
标签:场景 Python list Manager result 进程 append

需求:使用多进程,把add的结果放进list

原始的多进程之间不能共享数据
使用Manager来管理list,多进程可以操作同一个list
使用multiprocessing.Manager().list()创建一个list

def funAdd(result, a, b):
    r = a + b
    result.append(r)
def main_process():
    result = multiprocessing.Manager().list()
    for i in range(3): # 3个进程
        p = Process(target=funAdd, args=(result, 1, 3))
        p.start()
        p.join()
    return result #返回值

注意

  1. 传递的时候,要传入result
  2. 使用append()

标签:场景,Python,list,Manager,result,进程,append
From: https://www.cnblogs.com/NetUSA/p/16913651.html

相关文章