18.多进程
2018-09-10 本文已影响9人
芝麻酱的简书
学习网站:https://www.cnblogs.com/yuanchenqi/articles/6248025.html
进程创建使用:
方式1: 创建对象使用
from multiprocessing import Process
if __name__ == "__main__":
def showNum(num):
print(num)
p_list = []
for i in range(10):
p = Process(target=showNum, args=("abc",))
p_list.append(p)
p.start()
for i in p_list:
i.join()
方式2:使用继承的方式使用
进程间通信:
方式1:使用进程队列multiprocessing
import queue,time
import multiprocessing
def foo(q):
time.sleep(1)
print("son process",id(q))
q.put(123)
q.put("yuan")
if __name__ == '__main__':
#q=queue.Queue()
q=multiprocessing.Queue()
p=multiprocessing.Process(target=foo,args=(q,))
p.start()
#p.join()
print("main process",id(q))
print(q.get())
print(q.get())
方式2:管道
from multiprocessing import Process, Pipe
def f(conn):
conn.send([12, {"name":"yuan"}, 'hello'])
response=conn.recv()
print("response",response)
conn.close()
print("q_ID2:",id(conn))
if __name__ == '__main__':
parent_conn, child_conn = Pipe() #双向管道
print("q_ID1:",id(child_conn))
p = Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv()) # prints "[42, None, 'hello']"
parent_conn.send("儿子你好!")
p.join()
方式3:Managers
Queue和Pipe这是实现了数据交互,并没有实现数据共享,即一个进程去更改另一个进程中的数据。
from multiprocessing import Process, Manager
def f(d, l,n):
d[n] = '1' #{0:"1"}
d['2'] = 2 #{0:"1","2":2}
l.append(n) #[0,1,2,3,4, 0,1,2,3,4,5,6,7,8,9]
if __name__ == '__main__':
with Manager() as manager:
d = manager.dict()#{}
l = manager.list(range(5))#[0,1,2,3,4]
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()
进程同步:
使用multiprocessing模块下的lock
from multiprocessing import Process, Lock
import time
def f(l, i):
l.acquire()
time.sleep(1)
print('hello world %s' % i)
l.release()
if __name__ == '__main__':
lock = Lock()
for num in range(10):
Process(target=f, args=(lock, num)).start()
进程池:
from multiprocessing import Process,Pool
import time,os
def Foo(i):
time.sleep(1)
print(i)
print("son",os.getpid())
return "HELLO %s"%i
def Bar(arg):
print(arg)
# print("hello")
# print("Bar:",os.getpid())
if __name__ == '__main__':
pool = Pool(5)
print("main pid",os.getpid())
for i in range(100):
#pool.apply(func=Foo, args=(i,)) #同步接口
#pool.apply_async(func=Foo, args=(i,))
#回调函数: 就是某个动作或者函数执行成功后再去执行的函数
pool.apply_async(func=Foo, args=(i,),callback=Bar)
pool.close()
pool.join() # join与close调用顺序是固定的
print('end')