python多进程获取返回值

2023-01-31 02:01:37 获取 返回值 进程

https://segmentfault.com/q/1010000010403117/a-1020000010411306

1. multiprocessing.Process

def worker(procnum, return_dict):
    '''worker function'''
    print str(procnum) + ' represent!'
    return_dict[procnum] = procnum

if __name__ == '__main__':
    manager = Manager()
    return_dict = manager.dict()
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(target=worker, args=(i,return_dict))
        jobs.append(p)
        p.start()

    for proc in jobs:
        proc.join()
    # 最后的结果是多个进程返回值的集合
    print return_dict.values()

2. multiprocessing.Pool

p = multiprocessing.Pool(1)
rslt = p.map(test,('i',))
print rslt

相关文章