python 进程间通信

from multiprocessing import Process,Value,Array
def fun(n,a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d',0.0)
    arr = Array('i',range(10))
    p = Process(target=fun,args=(num,arr))
    p.start()
    p.join()
    print(num.value)
    print(arr[:])

 

 1 from multiprocessing import Process,Manager
 2 def fun(d,l):
 3     d[1]='1'
 4     d[2]=2
 5     d[0.25] = None
 6     l.reverse()
 7 
 8 if __name__ == '__main__':
 9     with Manager() as manager:
10         d = manager.dict()
11         l = manager.list(range(10))
12         p = Process(target=fun,args=(d,l))
13         p.start()
14         p.join()
15         print(d)
16         print(l)

 进程池

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 from multiprocessing import Pool
 4 import time
 5 
 6 def myfun(i):
 7     time.sleep(2)
 8     return i + 100
 9 def end_call(arg):
10     print('end_call'+ str(arg))
11 if __name__ == '__main__':
12     p = Pool(5)
13     for i in range(10):
14         p.apply_async(func=myfun,args=(i,),callback=end_call)
15     print('end')
16     p.close()
17     p.join()

 

posted @ 2017-03-03 08:09  Erick-LONG  阅读(240)  评论(0编辑  收藏  举报