Python 多进程multiprocessing

时间:2023-03-08 20:46:35

一、python多线程其实在底层来说只是单线程,因此python多线程也称为假线程,之所以用多线程的意义是因为线程不停的切换这样比串行还是要快很多。python多线程中只要涉及到io或者sleep就会切换线程。因此在io密集型的情况下可以用多线程。

二、python的多进程是直接调用原生进程,相当于直接调用硬件资源,可以实现多核的功能。

  1、启动两个进程

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-26
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process
import time def f(name):
time.sleep(2)
print("hello",name) if __name__ == "__main__":
p = Process(target=f,args=("bob",))
p2 = Process(target=f, args=("bob",))
p.start()
p2.start()
p.join()

  2、启动子进程

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-26
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process
import os def info(title):
print(title)
print("module name:",__name__)
print("parent process:",os.getppid())
print("process id:",os.getpid())
print("\n\n") def f(name):
info("\033[31;1mfunction f\033[0m")
print("hello",name) if __name__ == "__main__":
info("\033[32;1mmain process line\033[0m")
p = Process(target=f,args=("bob",))
p.start()
p.join()

  执行结果:

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
main process line
module name: __main__
parent process: 7308 #这是pycharm的进程号,也是主程序的主进程
process id: 9724  #这是主程序的进程 function f
module name: __mp_main__
parent process: 9724 #这是子进程的父进程,也就是主程序的进程
process id: 3108  #这是子进程id

三、进程间通讯。

  1、不同进程间内存是不共享的,要想实现两个进程间的数据交换,可以用以下方法:

    a、Queues,使用方法跟threading里的queue差不多

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-26
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process,Queue
#Queue相当于给数据加了一把锁,只能按顺序排队取,在进程中使用Queue def f(q):
q.put([42,None,"hello"]) if __name__ == "__main__":
q = Queue()
p = Process(target=f,args=(q,)) #在子进程里put一个数据
p.start()
print(q.get()) #在父进程中get
p.join()

  执行结果: 

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
[42, None, 'hello']

    b、多个进程向队列中写数据

#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-26
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process,Queue
#Queue相当于给数据加了一把锁,只能按顺序排队取,在进程中使用Queue def f(q):
q.put([42,None,"hello"]) if __name__ == "__main__":
q = Queue()
p = Process(target=f,args=(q,)) #在子进程里put一个数据
p2 = Process(target=f, args=(q,)) # 在子进程里put一个数据
p.start()
p2.start()
print("from parent",q.get()) #在父进程中get
print("from parent2",q.get())
print("from parent3",q.get())#因为已经被取完了所以没有了
p.join()

  执行结果:

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
from parent [42, None, 'hello']
from parent2 [42, None, 'hello']

四、Pipes,和Queue差不多,也可以一对多。

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-27
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process,Pipe def f(conn):
conn.send([42,None,"hello"])
conn.close() if __name__ == "__main__":
parent_conn,child_conn = Pipe()
p = Process(target=f,args=(child_conn,)) #子进程里send一个数据然后关闭
p2 = Process(target=f, args=(child_conn,)) # 子进程里send一个数据然后关闭
p.start()
p2.start()
print(parent_conn.recv()) #prints "[42,None,"hello"]"
print(parent_conn.recv())
p.join()

  执行结果:

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
[42, None, 'hello']
[42, None, 'hello'] Process finished with exit code 0

五、进程间实现数据共享 Manager

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-27
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 #进程间实现数据共享
from multiprocessing import Process,Manager #导入Manager def f(d,l,n):
d[n] = n
d[""] = 2
d[0.25] = None
l.append(n)
print(l) if __name__ == "__main__":
with Manager() as manager:
d = manager.dict()
l = manager.list(range(5))
p_list = []
for i in range(10):
p = Process(target=f,args=(d,l,i))
p.start()
p_list.append(p)
for res in p_list:
res.join()
print(d)
print(l)

执行结果:

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
[0, 1, 2, 3, 4, 3]
[0, 1, 2, 3, 4, 3, 2]
[0, 1, 2, 3, 4, 3, 2, 6]
[0, 1, 2, 3, 4, 3, 2, 6, 1]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0, 4]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0, 4, 9]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0, 4, 9, 5]
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0, 4, 9, 5, 8]
{3: 3, '': 2, 0.25: None, 2: 2, 6: 6, 1: 1, 7: 7, 0: 0, 4: 4, 9: 9, 5: 5, 8: 8}
[0, 1, 2, 3, 4, 3, 2, 6, 1, 7, 0, 4, 9, 5, 8] Process finished with exit code 0

六、进程同步

  python3默认进程会自动加锁,但是python2不会

 #!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-27
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process,Lock def f(l,i):
l.acquire()
try:
print("hello world",i)
finally:
l.release() if __name__ == "__main__":
lock = Lock()
for num in range(10):
Process(target=f,args=(lock,num)).start()

  执行结果:

 C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
hello world 3
hello world 6
hello world 2
hello world 7
hello world 0
hello world 5
hello world 1
hello world 4
hello world 8
hello world 9 Process finished with exit code 0

七、进程池

  1、进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可用进程,那么程序就会等待,直到进程池中有可用进程为止。

  2、进程池中有两个方法:

    a、apply 同步

    b、apply_async 异步

#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-11-27
# 邮箱:1209989516@qq.com
# 这是我用来练习python多进程的测试脚本 from multiprocessing import Process,Pool,freeze_support #在windows上需要导入freeze_support这个包使用,linux上不需要
import time def Foo(i):
time.sleep(2)
return i + 100 def Bar(arg):
print("--> exec done",arg) if __name__ == '__main__':
freeze_support()
pool = Pool(5) #允许最大五个进程同时运行 for i in range(10):
pool.apply_async(func=Foo,args=(i,),callback=Bar)
#callback是回调函数,就是函数Foo执行完了再执行Bar,Foo的执行结果会自动传给Bar,就相当于打印出Foo的执行结果
#pool.apply(func=Foo,args=(i,)) #这个是同步,是串行执行,同步的时候不能使用callback方法 print("end")
pool.close()
pool.join() #进程池中执行完毕后再关闭,如果注释,那么程序直接关闭,按理来说应该程序执行完后再关闭线程池,但是python里面的写法就是这样的

  执行结果:因为允许最大五个进程同时运行因此会每次先打印五个

C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day15/multiprocessing_learn.py
end
--> exec done 100
--> exec done 101
--> exec done 102
--> exec done 103
--> exec done 104
--> exec done 105
--> exec done 106
--> exec done 107
--> exec done 108
--> exec done 109 Process finished with exit code 0