Python多进程(multiprocessing)共享变量

Python多进程(multiprocessing)共享变量

#!/usr/bin/env python2
# coding: utf8
import multiprocessing

def foo(h,context):
    if h%2 == 0:
        print (h)
    else:
        context.append(h)

if __name__ == "__main__":
    ## 设置共享list
    con = multiprocessing.Manager().list()
    ## 设置进程池大小
    p = multiprocessing.Pool(2)
    for i in range(18):
        # con = multiprocessing.Manager().list()
        p.apply_async(foo,args=(i,con))
    p.close()
    p.join()
    print con

标题名称:Python多进程(multiprocessing)共享变量
当前地址:http://pwwzsj.com/article/gpshge.html