(七)7-5多进程

时间:2021-04-02 11:30:20


多进程和多线程的区别
多线程使用的是cpu的一个核,适合io密集型
多进程使用的是cpu的多个核,适合运算密集型

组件
Python提供非常好的多进程包如:multiprocessing
Multiprocessing支持紫禁城,通信,共享数据,执行不同形式的同步,提供是ProcessPipe,lock等组件

multiprocessing 常用的方法

# multiprocessing.active_children()   列出存在的子进程
# cpu_count()                           统计cpu个数
例:
import  multiprocessing
p = multiprocessing.cpu_count()
m = multiprocessing.active_children()
print  p
print m
运行结果:
4
[]

  

Process
1、创建一个Process对象
p = multiprocessing.Process(target=worker, args=(5,))
注:target=函数名字
args= 函数需要的参数,以tuple的形式传入
2、Process常用方法
is_alive() 判断进程是否存在
run() 启动进程
start() 启动进程,会自动调用run方法
join(timeout) 等待进程结束或超时
3、Process常用属性
name 进程名字
pid 进程的pid
例子:

import  multiprocessing
import  time
def worker(interval):
    time.sleep(interval)
    print "hell world!!!"

if __name__ == "__main__":
    p = multiprocessing.Process(target=worker, args=(5,))
    p.start()
    print p.pid
    print p.is_alive()
    p.join()
    # p.join(timeout=3)     #等待紫禁城执行完毕或者超时退出
    print "end main"
    print p.name
    print p.pid

  运行结果:

17348
True
hell world!!!
end main
Process-1
17348

  多进程实例:

import  time
from datetime import  datetime
import  multiprocessing
now = datetime.strftime(datetime.now(),"%Y-%m-%d %H:%M:%S")
def worker(name,interval):
    start = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
    print " {0} start {1}".format(name,start)
    time.sleep(interval)
    end = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")
    print " {0} end  {1}".format(name,end)

if __name__ == "__main__":
    print "main start"
    print  "this  computer has {0} cpu".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()
    print  multiprocessing.cpu_count()
    for p in multiprocessing.active_children():
        print "this pid of {0} is {1}".format(p.name,p.pid)
    print  "main end"

  运行结果:

main start
this  computer has 4 cpu
4
this pid of Process-1 is 19872
this pid of Process-3 is 19008
this pid of Process-2 is 18844
main end
 worker1 start 2017-12-08 23:40:12
 worker2 start 2017-12-08 23:40:12
 worker3 start 2017-12-08 23:40:12
 worker1 end  2017-12-08 23:40:14
 worker2 end  2017-12-08 23:40:15
 worker3 end  2017-12-08 23:40:16