多进程
概念:
进程是程序在计算机上的一次执行活动。当你运行一个程序,你就启动了一个进程。显然,程序是死的(静态的),进程是活的(动态的)。进程可以分为系统进程和用户进程。凡是用于完成操作系统的各种功能的进程就是系统进程,它们就是处于运行状态下的操作系统本身;用户进程就不必我多讲了吧,所有由你启动的进程都是用户进程。进程是操作系统进行资源分配的单位。
多进程和多线程的区别:
多线程使用的是cpu的一个核,适合io密集型
多进程使用的是cpu的多个核,适合运算密集型
组件:
Python提供了非常好用的多进程包,multiprocessing,我们在使用的时候,只需要导入该模块就可以了。
Multiprocessing支持子进程,通信,共享数据,执行不同形式的同步,提供了Process,Pipe, Lock等组件
Process
1. 创建一个Process对象
p = multiprocessing.Process(target=worker_1, args=(2, ))
target = 函数名字
args = 函数需要的参数,以tuple的形式传入
注意: 单个元素的tuple的表现形式
multprocessing用到的两个方法
cpu_count() 统计cpu总数
active_children() 获得所有子进程
Process的常用方法
is_alive() 判断进程是否存活
run() 启动进程
start() 启动进程,会自动调用run方法,这个常用
join(timeout) 等待进程结束或者直到超时
Process的常用属性
name 进程名字
pid 进程的pid
import multiprocessing # multiprocessing.active_children() 列出存在的子进程 # 1 ->2, 3, 4 #cpu_count() 统计cpu的个数 import time def worker(interval): time.sleep(interval) print("hello world") if __name__ == "__main__": p = multiprocessing.Process(target=worker, args=(5,)) p.start() print(p.is_alive()) p.join(timeout=3) #等待子进程执行完毕或者超时退出 print("end main") print(p.name) print(p.pid)
import multiprocessing import time def worker(name, interval): print("{0} start".format(name)) time.sleep(interval) print("{0} end".format(name)) if __name__ == "__main__": print("main start") print("this Computer has {0}".format(multiprocessing.cpu_count())) p1 = multiprocessing.Process(target=worker, args=("worker1", 2)) p2 = multiprocessing.Process(target=worker, args=("worker2", 3)) p3 = multiprocessing.Process(target=worker, args=("worker3", 4)) p1.start() p2.start() p3.start() for p in multiprocessing.active_children(): print("the pid of {0} is {1}".format(p.name, p.pid)) print("main end")
锁
import multiprocessing # lock = multiprocessing.Lock() # lock.acquire() 获取锁 # lock.release() 释放锁 # with lock: # 不加锁程序 # number +1 # number +3 import time def add(number, value, lock): lock.acquire() try: print("init add{0} number = {1}".format(value, number)) for i in xrange(1, 6): number += value time.sleep(1) print("add{0} number = {1}".format(value, number)) except Exception as e: raise e finally: lock.release() if __name__ == "__main__": lock = multiprocessing.Lock() number = 0 p1 = multiprocessing.Process(target=add, args=(number, 1, lock)) p2 = multiprocessing.Process(target=add, args=(number, 3, lock)) p1.start() p2.start() print("main end")
共享内存
import multiprocessing import time # Value() # Array() def add(number, add_value, lock): lock.acquire() try: print("init add{0} number = {1}".format(add_value, number.value)) for i in xrange(1, 6): number.value += add_value print("##############add{0} has added!############".format(add_value)) time.sleep(1) print("add{0} number = {1}".format(add_value, number.value)) except Exception as e: raise e finally: lock.release() def change(arr): for i in range(len(arr)): arr[i] = -arr[i] if __name__ == "__main__": lock = multiprocessing.Lock() number = multiprocessing.Value('i', 0) arr = multiprocessing.Array('i', range(10)) print(arr[:]) p1 = multiprocessing.Process(target=add, args=(number, 1, lock)) p2 = multiprocessing.Process(target=add, args=(number, 3, lock)) p3 = multiprocessing.Process(target=change, args=(arr,)) p1.start() p2.start() p3.start() p3.join() print(arr[:]) print("main end")
manage
import multiprocessing def worker(d, l): l += range(11, 16) for i in xrange(1, 6): key = "key{0}".format(i) val = "val{0}".format(i) d[key] = val if __name__ == "__main__": manager = multiprocessing.Manager() d = manager.dict() l = manager.list() p = multiprocessing.Process(target=worker, args=(d, l)) p.start() p.join() print(d) print(l) print("main end")
进程池
import multiprocessing import time def worker(msg): print("########start {0}##########".format(msg)) time.sleep(1) print("########end {0}##########".format(msg)) if __name__ == "__main__": print("main start") pool = multiprocessing.Pool(processes=3) for i in xrange(1, 10): msg = "hello {0}".format(i) pool.apply_async(func=worker, args=(msg,)) pool.close() pool.join() #在join之前,一定要调用close,否则报错。 print("main end")