Python菜鸟学习手册16----多进程
2016-04-21 22:36
681 查看
python下的多进程
在批评Python的讨论中,常常说起Python多线程是多么的难用。由于GIL的存在,python一个进程同时只能执行一个线程。因此在python开发时,计算密集型的程序常用多进程,IO密集型的使用多线程。multiprocessing
Process
类似threading下的Thread,创建一个Process是很简单的。#coding=utf-8 __author__ = 'a359680405' from multiprocessing import Process #方法1:将要执行的方法作为参数传给Process def f(name): print('hello', name) #方法2:从Process继承,并重写run() class MyProcess(Process): def run(self): print("MyProcess extended from Process") if __name__ == '__main__': #需要注意的是,多进程只能在main中执行 p1 = Process(target=f, args=('bob',)) p1.start() p2=MyProcess() p2.start()
构造方法:
Process(group=None, target=None, name=None, args=(), kwargs={})
group: 进程组,目前还没有实现,库引用中提示必须是None;
target: 要执行的方法;
name: 进程名;
args/kargs: 要传入方法的参数。
实例方法:
run(): 默认的run()函数调用target的函数,你也可以在子类中覆盖该函数。
start() : 启动该进程。
join([timeout]) : 父进程被停止,直到子进程被执行完毕。当timeout为None时没有超时,否则有超时。
is_alive(): 返回进程是否在运行。正在运行指启动后、终止前。
terminate():结束进程。
实例属性:
name :进程名
daemon : 守护进程
pid : 进程ID
#coding=utf-8 __author__ = 'a359680405' from multiprocessing import Process from threading import Thread import time import os def foo(n): time.sleep(2) print("报数:",n) print("子进程ID",os.getpid(),",父进程ID",os.getppid()) def main1(): for i in range(2): foo(i) def main2(): for i in range(2): p=Process(target=foo,args=(i,)) print(p.name,"准备执行") #p.name为进程名 p.start() print(p.pid,"开始执行") #在进程start前,进程号p.pid为None p.join(1) #join([timeout]) 父进程被停止,直到子进程被执行完毕。 def main3(): for i in range(2): p=Thread(target=foo,args=(i,)) p.start() if __name__ == '__main__': print("主进程ID",os.getpid()) #单进程执行, 子进程与主进程是同一个进程 #main1() main2() #多进程执行,子进程id不同,父进程都是主进程 #main3() #多线程执行,子线程获取的进程号与主进程相同
Lock
multiprocessing.Lock的用法与threading.Lock用法相同。在下面例子中,如果两个进程没有使用lock来同步,则他们对同一个文件的写操作可能会出现混乱。当然一般可以用文件锁控制多个进程对同一文件的读写。import multiprocessing import sys def worker_with(lock, f): with lock: fs = open(f,"a+") fs.write('Lock acquired via with\n') fs.close() def worker_no_with(lock, f): lock.acquire() try: fs = open(f,"a+") fs.write('Lock acquired directly\n') fs.close() finally: lock.release() if __name__ == "__main__": f = "file.txt" lock = multiprocessing.Lock() w = multiprocessing.Process(target=worker_with, args=(lock, f)) nw = multiprocessing.Process(target=worker_no_with, args=(lock, f)) w.start() nw.start() w.join() nw.join()
Semaphore
Semaphore用来控制对共享资源的访问数量,例如池的最大连接数。#coding=utf-8 __author__ = 'a359680405' import multiprocessing import time def worker(s,i): s.acquire() print(multiprocessing.current_process().name + " acquire") time.sleep(i) print(multiprocessing.current_process().name + " release") s.release() if __name__ == "__main__": s = multiprocessing.Semaphore(2) #最多同时执行两个进程 for i in range(5): p = multiprocessing.Process(target=worker, args=(s,i*2)) p.start()
Event
Event用来实现进程间同步通信。#coding=utf-8 __author__ = 'a359680405' import multiprocessing import time def wait_for_event(e): """Wait for the event to be set before doing anything""" print ('wait_for_event: starting') e.wait() print ('wait_for_event: e.is_set()->' + str(e.is_set())) def wait_for_event_timeout(e, t): """Wait t seconds and then timeout""" print ('wait_for_event_timeout: starting') e.wait(t) #等两秒钟set,等不到就不等了 print ('wait_for_event_timeout: e.is_set()->' + str(e.is_set())) if __name__ == '__main__': e = multiprocessing.Event() w1 = multiprocessing.Process(name='block', target=wait_for_event, args=(e,)) w1.start() w2 = multiprocessing.Process(name='non-block', target=wait_for_event_timeout, args=(e, 2)) w2.start() time.sleep(3) e.set() print ('main: event is set')
进程间的数据共享
进程同步,默认是不同步的。比如下列程序。一般通过使用特殊结构进行进程间的数据共享。#coding=utf-8 __author__ = 'a359680405' from multiprocessing import Process from threading import Thread import time def foo(info_list,n): info_list.append(n) print(info_list) if __name__ == '__main__': info_list=[] for i in range(10): p=Process(target=foo,args=(info_list,i,)) p.start() #输出[0],[1].... # p=Thread(target=foo,args=(info_list,i,)) #输出[0],[0,1],[0,1,2]..... # p.start()
上面例子,进程会输出[0][1][3][2]。。。。。。。。。。。
而线程会输出[0],[0,1],[0,1,2]…..
multiprocessing.Queue
利用multiprocess中的Queue进行进程数据共享。#coding=utf-8 __author__ = 'a359680405' """注意这里的Queue用的不是queue中的Queue!!Queue.Queue是进程内非阻塞队列, multiprocess.Queue是跨进程通信队列。多进程前者是各自私有,后者是各子进程共有。""" from multiprocessing import Process,Queue def foo(q,n): q.put(n) if __name__ == '__main__': que=Queue() for i in range(5): p=Process(target=foo,args=(que,i)) p.start() p.join() print(que.qsize())
multiprocessing.Value与multiprocessing.Array
利用multiprocessing.Value与multiprocessing.Array进行数据共享#coding=utf-8 __author__ = 'a359680405' from multiprocessing import Process,Value,Array def foo1(n,a): n.value=3 for i in range(len(a)): a[i]=-a[i] if __name__ == '__main__': num=Value("d",0.0) #d的意思是小数.创建0.0 arr=Array("i",range(10)) #i的意思是整数.创建一个0-9的整数 p=Process(target=foo1,args=(num,arr)) p.start() p.join() print(num.value) print(arr[:])
multiprocessing.Manager
Value、Array比Manager稍微快一些,支持的类型比Manager要少。因此,一般情况下,用Queue和Manager。下面是使用Manager进行数据共享。#coding=utf-8 __author__ = 'a359680405' from multiprocessing import Process,Manager def f(d,l): d[1]="1" d["2"]=2 d[0.25]=None l.reverse() if __name__ == '__main__': manager=Manager() d=manager.dict() #创建一个进程间可共享的dict l=manager.list(range(10)) #创建一个进程间可共享的list p=Process(target=f,args=(d,l)) p.start() p.join() print(d) #输出{0.25: None, 1: '1', '2': 2} print(l) #输出[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
pool
如果需要多个子进程时可以考虑使用进程池(pool)来管理.from multiprocessing import Pool import time def foo(x): x+=x time.sleep(2) return x if __name__ == '__main__': p=Pool(2) res_list=[] for i in range(10): res=p.apply_async(foo,[i,]) #异步。 将10个进程放到进程池里。默认已经启动。如果用p.apply()就会阻塞。 # res.get() #这样会进程阻塞,一个一个的启动。所以不能把res。get()放在这里 res_list.append(res) for i in res_list: print(i.get(timeout=3)) #可以发现两个两个的执行。如果等待时间超过3秒就会报错 #print(pool.map(f,range(10))) #上面10到17行相当于本句
本文地址:http://blog.csdn.net/a359680405/article/details/51213682
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例