<--目录-->
1、多进程
多进程官方文档
https://docs.python.org/2/library/multiprocessing.html
【多进程】
示例1:
#
!/usr/bin/env python #coding:utf-8 from multiprocessing import Pool def f(x): return x*x #返回自乘值 if __name__ == ‘__main__‘: p = Pool(5) #定义5个进程 print (p.map(f,[1,2,3])) #调用f函数,把1,2.3传值给x,让他们自乘 示例2:(此脚本只能在Linux下使用产生效果) #多进程使用方法: #!/usr/bin/env python #coding:utf-8 from multiprocessing import Process import os import time def info(title): print title print ‘module name:‘,__name__ if hasattr(os, ‘getppid‘): print ‘parent process:‘, os.getppid() #输出父进程ID time.sleep(3) print ‘process id:‘, os.getpid() #输出子进程ID def f(name): info(‘function f‘) print ‘hellow‘,name if __name__ == ‘__main__‘: info(‘main line‘) print ‘---------------‘ p = Process(target=f,args=(‘bob‘,)) p.start() p.join()
使用方法:多少个核CPU就用多少个进程,或者用CPU核数的1倍
多进程使用和多线程使用不同点(此脚本只能在Linux下跑) [[email protected] opt]# cat pro.py #!/usr/bin/env python #coding:utf-8 from multiprocessing import Process import threading def run(info_list,n): info_list.append(n) print info_list info=[] for i in range(10): p = Process(target=run,args=[info,i]) #多进程独享内存,十个进程 p.start() print ‘---------threding-----‘ info=[] for i in range(10): p = threading.Thread(target=run,args=[info,i]) #多线程共享内存,十个线程 p.start()
进程间的内存同步方法:(内存共享第一种方法)
[[email protected] opt]# vim multiprocess.py #!/usr/bin/env python #coding:utf-8 from multiprocessing import Process,Queue def f(q,n): q.put([n,‘hello‘]) print q.get() print q.get() if __name__ == ‘__main__‘: q = Queue() q.put(‘ddd‘) for i in range(5): p = Process(target=f,args=(q,i)) p.start() while True: print q.get()
内存共享的第二种方法
#!/usr/bin/env python #coding:utf-8 from multiprocessing import Process, Value, Array def f(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=f, args=(num, arr)) p.start() p.join() print num.value print arr[:]
内存共享的第三种方法 (推荐使用,支持的类型比较多)
#!/usr/bin/env python #coding:utf-8 from multiprocessing import Process,Manager def f(d,l): d[1] = ‘1‘ #给字典赋值 d[2] = ‘2‘ d[0.25] = None l.reverse #把列表反向输出 if __name__ == ‘__main__‘: manger = Manager() d = manger.dict() #获取字典,空字典 l = manger.list(range(10)) #获取列表 p = Process(target=f, args=(d,l)) p.start() p.join() print d print l
通过Pool产生多进程,进程池Pool (不常用)
#!/usr/bin/env python #coding:utf-8 from multiprocessing import Pool import time def f(x): print x*x time.sleep(2) return x*x pool = Pool(processes=5) #每次跑5个进程 res_list = [] for i in range(10): res = pool.apply_async(f, [i,]) #定义十个进程 #res = Process(target=f, args[i,]) #上面那一句相当于这一句的意思 #print ‘----:‘,i res_list.append(res) for r in res_list: print r.get() #print pool.map(f.range(10))
#进程生成线程
#!/usr/bin/env python #coding:utf-8 from multiprocessing import Pool import time def f(x): info_list = [] for i in range()5: t = threading.Thread(target =t_func,args=[info_list,i]) t.start() pool = Pool(processes=5) #每次跑5个进程 res_list = [] for i in range(10): res = pool.apply_async(f, [i,]) #定义十个进程 #res = Process(target=f, args[i,]) #上面那一句相当于这一句的意思 print ‘----:‘,i res_list.append(res) for r in res_list: print r.get(timeout=1) #print pool.map(f.range(10)) 1、创建进程程序 from multiprocessing import Process import threading import time def Foo(i): print ‘say hi‘,i for i in range(10): p = Process(target=Foo,args=(i,)) p.start()
2、进程间的数据共享
默认各自持有一份
复制代码
#!/usr/bin/env python #coding:utf-8 from multiprocessing import Process from multiprocessing import Manager import time li = [] def Foo(i): li.append(i) print ‘say hi‘,li for i in range(10): p = Process(target=Foo,args=(i,)) p.start() print li
通过特殊的数据结构,可以完成进程间的数据共享
复制代码
#方法一,Array from multiprocessing import Process,Array temp = Array(‘i‘, [11,22,33,44]) def Foo(i): temp[i] = 100+i for item in temp: print i,‘----->‘,item for i in range(2): p = Process(target=Foo,args=(i,)) p.start() p.join()
#方法二:manage.dict()共享数据
from multiprocessing import Process,Manager manage = Manager() dic = manage.dict() def Foo(i): dic[i] = 100+i print dic.values() for i in range(2): p = Process(target=Foo,args=(i,)) p.start() p.join()
#方法三:manage.Namespace()共享数据
from multiprocessing import Process,Manager manage = Manager() namespace = manage.Namespace() namespace.x = [11,22,33] def Foo(i,dic): namespace.x = [11,22,33,i] print namespace for i in range(2): p = Process(target=Foo,args=(i,namespace)) p.start() p.join()
时间: 2024-10-26 12:13:09